numpy.std(arr,axis = None):計算指定數據(數組元素)沿指定軸(如果有)的標準偏差。
標準差(SD)度量為給定數據集中數據分布的傳播程度。
例如:
x = 1 1 1 1 1 Standard Deviation = 0 . y = 9, 2, 5, 4, 12, 7, 8, 11, 9, 3, 7, 4, 12, 5, 4, 10, 9, 6, 9, 4 Step 1 : Mean of distribution 4 = 7 Step 2 : Summation of (x - x.mean())**2 = 178 Step 3 : Finding Mean = 178 /20 = 8.9 This Result is Variance. Step 4 : Standard Deviation = sqrt(Variance) = sqrt(8.9) = 2.983..
參數:
arr :[數組]輸入數組。
axis :我們要沿其計算標準差的[int或int元組]。否則,它將認為arr是平坦的(在所有軸上均有效)。軸= 0表示沿列的SD,軸= 1表示沿行的SD。
out :[ndarray,可選]我們要在其中放置結果的不同數組。數組必須具有與預期輸出相同的尺寸。
dtype :[數據類型,可選]我們在計算SD時需要的類型。
Results :數組的標準偏差(如果沒有軸則為標量值)或沿指定軸具有標準偏差值的數組。
代碼1:
# Python Program illustrating
# numpy.std() method
import numpy as np
# 1D array
arr = [20, 2, 7, 1, 34]
print("arr : ", arr)
print("std of arr : ", np.std(arr))
print ("\nMore precision with float32")
print("std of arr : ", np.std(arr, dtype = np.float32))
print ("\nMore accuracy with float64")
print("std of arr : ", np.std(arr, dtype = np.float64))
輸出:
arr : [20, 2, 7, 1, 34] std of arr : 12.576167937809991 More precision with float32 std of arr : 12.576168 More accuracy with float64 std of arr : 12.576167937809991
代碼2:
# Python Program illustrating
# numpy.std() method
import numpy as np
# 2D array
arr = [[2, 2, 2, 2, 2],
[15, 6, 27, 8, 2],
[23, 2, 54, 1, 2, ],
[11, 44, 34, 7, 2]]
# std of the flattened array
print("\nstd of arr, axis = None : ", np.std(arr))
# std along the axis = 0
print("\nstd of arr, axis = 0 : ", np.std(arr, axis = 0))
# std along the axis = 1
print("\nstd of arr, axis = 1 : ", np.std(arr, axis = 1))
輸出:
std of arr, axis = None : 15.3668474320532 std of arr, axis = 0 : [ 7.56224173 17.68473918 18.59267329 3.04138127 0. ] std of arr, axis = 1 : [ 0. 8.7772433 20.53874388 16.40243884]
相關用法
注:本文由純淨天空篩選整理自Mohit Gupta_OMG 大神的英文原創作品 numpy.std() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。