np.var()函数:
def var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue):
axis : None or int or tuple of ints, optional
Axis or axes along which the variance is computed. The default is to
compute the variance of the flattened array.
这里说明axis值为整数或元组(类似于(1,2,3))这种
对于二维矩阵,axis的值可以是0也可以是1,其中axis=0表示按列求平均,当axis=1表示按行求平均,未给出axis则表示将所有元素加起来求平均。
dataset = [
[1.658985, 4.285136, -3.453687, 3.424321, 4.838138, -1.151539, -5.379713, -3.362104, 0.972564, 2.924086],
[-3.567919, 1.531611, 0.450614, -3.302219, -3.487105, -1.724432, 2.668759, 1.594842, -3.156485, 3.191137],
[3.165506, -3.999838, -2.786837, -3.099354, 4.208187, 2.984927, -2.123337, 2.943366, 0.704199, -0.479481],
[-0.39237, -3.963704, 2.831667, 1.574018, -0.790153, 3.343144, 2.943496, -3.357075, -3.195883, -2.283926],
[2.336445, 2.875106, -1.786345, 2.554248, 2.190101, -1.90602, -3.403367, -2.778288, 1.778124, 3.880832],
[-1.688346, 2.230267, 2.592976, -2.054368, -4.007257, -3.207066, 2.257734, 3.387564, -2.679011, 0.785119],
[0.939512, -4.023563, -3.674424, -2.261084, 2.046259, 2.735279, -3.18947, 1.780269, 4.372646, -0.822248]]
print(var(dataset,axis=0))
输出结果:
[ 4.85572291 11.66931904 6.64039908 7.02010573 10.76766748
6.51731034 10.0788433 8.01974819 7.26723794 4.73381655]
print(var(dataset,axis=1))
输出结果:
[ 11.69812548 6.77643084 8.36777452 7.2442908 6.6098529
6.89108237 8.0053096 ]
print(var(dataset))
输出结果:
8.10196690528