Python Numpy.std()-標準偏差関数
    
    
            Sohaib Atiq
    2023年1月30日
    
    NumPy
    
 
Numpy.std() 関数は、指定された配列の指定された軸に沿った標準偏差を計算します。
numpy.std() の構文
numpy.std(arr, axis=None, dtype=float64)
パラメーター
| arr | array_like 標準偏差を計算する入力配列 | 
| axis | None、 int、または要素intを含むタプル軸。axis = 0は列に沿って標準偏差を計算することを意味し、 axis = 1は行に沿って標準偏差を計算することを意味します。axisが指定されていない場合、多次元配列はフラットリストとして扱われます。 | 
| dtype | dtypeまたはNone標準偏差の計算中に使用されるデータ型。 | 
戻り値
指定された配列の標準偏差、または指定された軸に沿った標準偏差を持つ配列を返します。
コード例:1 次元配列の numpy.std()
Python 1 次元配列が入力である場合、Numpy.std() 関数は配列内のすべての値の標準偏差を計算します。
import numpy as np
arr = [10, 20, 30]
print("1-D array :", arr)
print("Standard Deviation of arr is ", np.std(arr))
出力:
1-D array : [10, 20, 30]
Standard deviation of arr is  8.16496580927726
ここで、1 次元配列には 10、20、30 の要素があります。したがって、返される DataFrame の値は、軸情報を割り当てない標準偏差です。
コード例:2 次元配列の numpy.std()
import numpy as np
arr = [[10, 20, 30], [3, 50, 5], [70, 80, 90], [100, 110, 120]]
print("Two Dimension array :", arr)
print("SD of with no axis :", np.std(arr))
print("SD of with axis along column :", np.std(arr, axis=0))
print("SD of with axis aong row :", np.std(arr, axis=1))
出力:
Two Dimension array : [[10, 20, 30], [3, 50, 5], [70, 80, 90], [100, 110, 120]]
SD of with no axis : 41.21960159384798
SD of with axis along column : [40.73312534 33.54101966 45.87687326]
SD of with axis aong row : [ 8.16496581 21.6999744   8.16496581  8.16496581]
np.std(arr) は入力配列を平坦化された配列として扱い、この 1 次元の平坦化された配列の標準偏差を計算します。
np.std(arr、axis = 0) は列に沿った標準偏差を計算します。入力配列の各列の標準偏差として [40.73312534 33.54101966 45.87687326] を返します。
np.std(arr、axis = 1) は行に沿った標準偏差を計算します。入力配列の各行の標準偏差として [8.16496581 21.6999744 8.16496581 8.16496581] を返します。
コード例:dtype が指定された numpy.std()
import numpy as np
arr = [10, 20, 30]
print("Single Dimension array :", arr)
print("SD of Single Dimension array :", np.std(arr))
print("SD value with float32 data :", np.std(arr, dtype=np.float32))
print("SD value with float64 data :", np.std(arr, dtype=np.float64))
出力:
Single Dimension array : [10, 20, 30]
SD of Single Dimension array : 8.16496580927726
SD value with float32 data : 8.164966
SD value with float64 data : 8.16496580927726
numpy.std() 関数で dtype パラメーターが指定されている場合、標準偏差の計算中に指定されたデータ型が使用されます。
dtype に float64 ではなく float32 を割り当てると、標準偏差の解像度が低くなることは明らかです。

        チュートリアルを楽しんでいますか? <a href="https://www.youtube.com/@delftstack/?sub_confirmation=1" style="color: #a94442; font-weight: bold; text-decoration: underline;">DelftStackをチャンネル登録</a> して、高品質な動画ガイドをさらに制作するためのサポートをお願いします。 Subscribe