numpy.nanvar() method in Python
In this article, we will be learning about numpy.nanvar() method in Python. nanvar() is a function in NumPy module.
Definition:- the nanvar() function calculates the variance of the given data or an array data structure along with the specified axis, either row or column, by ignoring all NaN values.
To clarify, the variance is the average of the squared deviations from the mean, i.e., var =mean(abs(x-x.mean())**2).
Syntax:- numpy.nanvar(a, axis = None, dtype = None, out = None, ddof = 0, keepdims=<no value>)
- a = array_like — Given data in array form.
- axis = int, a tuple of ints, None – optional — Axis or axes along which variance is computed.
- dtype = data type -optional — Type of data to be used in variance calculations. By default, it is float64.
- out = ndarray -optional — Alternate array to store the output. It must have the same shape as the initial array.
- ddof = int -optional –Delta Degrees Of Freedom: divisor is used in N – ddof, where N is the number of non – NaN values.
- keepdims = bool -optional — If true, the reduced axes are left in output array with size one dimension. The result is broadcasted correctly against the initial array.
Consequently, it returns:- variance of the input array.
Examples of numpy.nanvar() method in Python
Firstly, let us find the variance of a 1d array with and without NaN values:-
import numpy as np a = np.array([12,25,np.nan,55]) print(np.var(a),np.nanvar(a))
As a result, the following output is generated:-
Secondly, let us find the variance of a 2d array on various axes with var() and nanvar():-
import numpy as np b = np.array([[1,2,3],[4,np.nan,5],[np.nan,7,8]]) print(np.var(b),np.nanvar(b)) print(np.nanvar(b,axis = 0)) print(np.nanvar(b,axis = 1)) print(np.var(b,axis=0)) print(np.var(b,axis=1))
Consequently, the output is:-
nan 5.63265306122449 [2.25 , 6.25 ,4.22222222] [0.66666667, 0.25 ,0.25 ] [ nan, nan, 4.22222222] [0.66666667, nan, nan]
As you can see above, we get different results when we change the axis.