numpy.std() 用法及代码示例
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]
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。