numpy.var(arr, axis = None)
:计算指定数据(数组元素)沿指定轴(如果有)的方差。
例:
x = 1 1 1 1 1
Standard Deviation = 0 . Variance = 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.
参数:
-
arr :[数组]输入数组。
axis :我们要沿其计算方差的[int或int元组]轴。否则,它将考虑arr
展平(在所有轴上工作)。 axis = 0表示沿列的方差,而axis = 1表示沿行的方差。
out :[ndarray,可选]我们要在其中放置结果的另一个数组。数组必须具有与预期输出相同的尺寸。
dtype :[数据类型,可选]计算方差时需要的类型。 - Results :数组的方差(如果轴为空则为标量值)或具有沿指定轴的方差值的数组。
代码1:
# Python Program illustrating
# numpy.var() method
import numpy as np
# 1D array
arr = [20, 2, 7, 1, 34]
print("arr : ", arr)
print("var of arr : ", np.var(arr))
print("\nvar of arr : ", np.var(arr, dtype = np.float32))
print("\nvar of arr : ", np.var(arr, dtype = np.float64))
输出:
arr : [20, 2, 7, 1, 34] var of arr : 158.16 var of arr : 158.16 var of arr : 158.16
代码2:
# Python Program illustrating
# numpy.var() 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]]
# var of the flattened array
print("\nvar of arr, axis = None : ", np.var(arr))
# var along the axis = 0
print("\nvar of arr, axis = 0 : ", np.var(arr, axis = 0))
# var along the axis = 1
print("\nvar of arr, axis = 1 : ", np.var(arr, axis = 1))
输出:
var of arr, axis = None : 236.14000000000004 var of arr, axis = 0 : [ 57.1875 312.75 345.6875 9.25 0. ] var of arr, axis = 1 : [ 0. 77.04 421.84 269.04]
相关用法
注:本文由纯净天空筛选整理自Mohit Gupta_OMG 大神的英文原创作品 numpy.var() in Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。