📜  Python中的 numpy.var()

📅  最后修改于: 2022-05-13 01:55:20.170000             🧑  作者: Mango

Python中的 numpy.var()

numpy.var(arr, axis = None)计算给定数据(数组元素)沿指定轴(如果有)的方差。

例子 :

参数 :

代码#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]