numpy.nanvar(arr, axis = None)
:計算指定數據(數組元素)沿指定軸(如果有)的方差,而忽略NaN值。
例:
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 :數組的方差(如果沒有軸則為標量值)或具有沿指定軸的方差值的數組;而忽略NaN值。
代碼1:
# Python Program illustrating
# numpy.nanvar() method
import numpy as np
# 1D array
arr = [20, 2, np.nan, 1, 34]
print("arr : ", arr)
print("\nnanvar of arr : ", np.nanvar(arr))
print("var of arr : ", np.var(arr))
print("\nnanvar of arr : ", np.nanvar(arr, dtype = np.float32))
print("var of arr : ", np.var(arr, dtype = np.float32))
輸出:
arr : [20, 2, nan, 1, 34] nanvar of arr : 187.1875 var of arr : nan nanvar of arr : 187.1875 var of arr : nan
代碼2:
# Python Program illustrating
# numpy.nanvar() method
import numpy as np
# 2D array
arr = [[2, 2, 2, 2, 2],
[15, 6, np.nan, 8, 2],
[23, 2, 54, 1, 2, ],
[np.nan, 44, 34, 7, 2]]
# nanvar of the flattened array
print("\nnanvar of arr, axis = None : ", np.nanvar(arr))
print("\nvar of arr, axis = None : ", np.var(arr))
# nanvar along the axis = 0
print("\nnanvar of arr, axis = 0 : \n", np.nanvar(arr, axis = 0))
print("\nvar of arr, axis = 0 : ", np.var(arr, axis = 0))
# nanvar along the axis = 1
print("\nnanvar of arr, axis = 1 : ", np.nanvar(arr, axis = 1))
輸出:
nanvar of arr, axis = None : 249.88888888888889 var of arr, axis = None : nan nanvar of arr, axis = 0 : [ 74.88888889 312.75 458.66666667 9.25 0. ] var of arr, axis = 0 : [ nan 312.75 nan 9.25 0. ] nanvar of arr, axis = 1 : [ 0. 22.1875 421.84 313.1875]
相關用法
注:本文由純淨天空篩選整理自Mohit Gupta_OMG 大神的英文原創作品 numpy.nanvar() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。