dpnp.nanvar

dpnp.nanvar(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False, *, where=True, mean=None, correction=None)

Compute the variance along the specified axis, while ignoring NaNs.

For full documentation refer to numpy.nanvar.

Parameters:
  • a ({dpnp.ndarray, usm_ndarray}) -- Input array.

  • axis ({None, int, tuple of ints}, optional) --

    Axis or axes along which the variances must be computed. If a tuple of unique integers is given, the variances are computed over multiple axes. If None, the variance is computed over the entire array.

    Default: None.

  • dtype ({None, str, dtype object}, optional) --

    Type to use in computing the variance. By default, if a has a floating-point data type, the returned array will have the same data type as a. If a has a boolean or integral data type, the returned array will have the default floating point data type for the device where input array a is allocated.

    Default: None.

  • out ({None, dpnp.ndarray, usm_ndarray}, optional) --

    Alternative output array in which to place the result. It must have the same shape as the expected output but the type (of the calculated values) will be cast if necessary.

    Default: None.

  • ddof ({int, float}, optional) --

    Means Delta Degrees of Freedom. The divisor used in calculations is N - ddof, where N represents the number of non-NaN elements.

    Default: 0.0.

  • keepdims ({None, bool}, optional) --

    If True, the reduced axes (dimensions) are included in the result as singleton dimensions, so that the returned array remains compatible with the input array according to Array Broadcasting rules. Otherwise, if False, the reduced axes are not included in the returned array.

    Default: False.

  • mean ({dpnp.ndarray, usm_ndarray}, optional) --

    Provide the mean to prevent its recalculation. The mean should have a shape as if it was calculated with keepdims=True. The axis for the calculation of the mean should be the same as used in the call to this nanvar function.

    Default: None.

  • correction ({None, int, float}, optional) --

    Array API compatible name for the ddof parameter. Only one of them can be provided at the same time.

    Default: None.

Returns:

out -- An array containing the variances. If the variance was computed over the entire array, a zero-dimensional array is returned. If ddof is >= the number of non-NaN elements in a slice or the slice contains only NaNs, then the result for that slice is NaN.

Return type:

dpnp.ndarray

Limitations

Parameters where is only supported with its default value. Otherwise NotImplementedError exception will be raised.

Notes

The variance is the average of the squared deviations from the mean, that is var = mean(abs(x - x.mean())**2).

The mean is normally calculated as x.sum() / N, where N = len(x). If, however, ddof is specified, the divisor N - ddof is used instead. In standard statistical practice, ddof=1 provides an unbiased estimator of the variance of a hypothetical infinite population. ddof=0 provides a maximum likelihood estimate of the variance for normally distributed variables.

Note that, for complex numbers, the absolute value is taken before squaring, so that the result is always real and non-negative.

See also

dpnp.var

Compute the variance along the specified axis.

dpnp.std

Compute the standard deviation along the specified axis.

dpnp.nanmean

Compute the arithmetic mean along the specified axis, ignoring NaNs.

dpnp.nanstd

Compute the standard deviation along the specified axis, while ignoring NaNs.

Examples

>>> import dpnp as np
>>> a = np.array([[1, np.nan], [3, 4]])
>>> np.nanvar(a)
array(1.55555556)
>>> np.nanvar(a, axis=0)
array([1., 0.])
>>> np.nanvar(a, axis=1)
array([0.  , 0.25])  # may vary

Using the mean keyword to save computation time:

>>> a = np.array([[14, 8, np.nan, 10], [7, 9, 10, 11], [np.nan, 15, 5, 10]])
>>> mean = np.nanmean(a, axis=1, keepdims=True)
>>> np.nanvar(a, axis=1, mean=mean)
array([ 6.22222222,  2.1875    , 16.66666667])