Functions that ignore NaN.
nanmin
-- minimum non-NaN valuenanmax
-- maximum non-NaN valuenanargmin
-- index of minimum non-NaN valuenanargmax
-- index of maximum non-NaN valuenansum
-- sum of non-NaN valuesnanprod
-- product of non-NaN valuesnancumsum
-- cumulative sum of non-NaN valuesnancumprod
-- cumulative product of non-NaN valuesnanmean
-- mean of non-NaN valuesnanvar
-- variance of non-NaN valuesnanstd
-- standard deviation of non-NaN valuesnanmedian
-- median of non-NaN valuesnanquantile
-- qth quantile of non-NaN valuesnanpercentile
-- qth percentile of non-NaN valuesVariable | array_function_dispatch |
Undocumented |
Function | _copyto |
Replace values in a with NaN where mask is True. This differs from copyto in that it will deal with the case where a is a numpy scalar. |
Function | _divide_by_count |
No summary |
Function | _nan_mask |
No summary |
Function | _nanargmax_dispatcher |
Undocumented |
Function | _nanargmin_dispatcher |
Undocumented |
Function | _nancumprod_dispatcher |
Undocumented |
Function | _nancumsum_dispatcher |
Undocumented |
Function | _nanmax_dispatcher |
Undocumented |
Function | _nanmean_dispatcher |
Undocumented |
Function | _nanmedian |
Private function that doesn't support extended axis or keepdims. These methods are extended to this function using _ureduce See nanmedian for parameter usage |
Function | _nanmedian1d |
Private function for rank 1 arrays. Compute the median ignoring NaNs. See nanmedian for parameter usage |
Function | _nanmedian_dispatcher |
Undocumented |
Function | _nanmedian_small |
sort + indexing median, faster for small medians along multiple dimensions due to the high overhead of apply_along_axis |
Function | _nanmin_dispatcher |
Undocumented |
Function | _nanpercentile_dispatcher |
Undocumented |
Function | _nanprod_dispatcher |
Undocumented |
Function | _nanquantile_1d |
Private function for rank 1 arrays. Compute quantile ignoring NaNs. See nanpercentile for parameter usage |
Function | _nanquantile_dispatcher |
Undocumented |
Function | _nanquantile_unchecked |
Assumes that q is in [0, 1], and is an ndarray |
Function | _nanquantile_ureduce_func |
Private function that doesn't support extended axis or keepdims. These methods are extended to this function using _ureduce See nanpercentile for parameter usage |
Function | _nanstd_dispatcher |
Undocumented |
Function | _nansum_dispatcher |
Undocumented |
Function | _nanvar_dispatcher |
Undocumented |
Function | _remove_nan_1d |
Equivalent to arr1d[~arr1d.isnan()], but in a different order |
Function | _replace_nan |
No summary |
Function | nanargmax |
Return the indices of the maximum values in the specified axis ignoring NaNs. For all-NaN slices ValueError is raised. Warning: the results cannot be trusted if a slice contains only NaNs and -Infs. |
Function | nanargmin |
Return the indices of the minimum values in the specified axis ignoring NaNs. For all-NaN slices ValueError is raised. Warning: the results cannot be trusted if a slice contains only NaNs and Infs. |
Function | nancumprod |
Return the cumulative product of array elements over a given axis treating Not a Numbers (NaNs) as one. The cumulative product does not change when NaNs are encountered and leading NaNs are replaced by ones. |
Function | nancumsum |
Return the cumulative sum of array elements over a given axis treating Not a Numbers (NaNs) as zero. The cumulative sum does not change when NaNs are encountered and leading NaNs are replaced by zeros. |
Function | nanmax |
Return the maximum of an array or maximum along an axis, ignoring any NaNs. When all-NaN slices are encountered a RuntimeWarning is raised and NaN is returned for that slice. |
Function | nanmean |
Compute the arithmetic mean along the specified axis, ignoring NaNs. |
Function | nanmedian |
Compute the median along the specified axis, while ignoring NaNs. |
Function | nanmin |
Return minimum of an array or minimum along an axis, ignoring any NaNs. When all-NaN slices are encountered a RuntimeWarning is raised and Nan is returned for that slice. |
Function | nanpercentile |
Compute the qth percentile of the data along the specified axis, while ignoring nan values. |
Function | nanprod |
Return the product of array elements over a given axis treating Not a Numbers (NaNs) as ones. |
Function | nanquantile |
Compute the qth quantile of the data along the specified axis, while ignoring nan values. Returns the qth quantile(s) of the array elements. |
Function | nanstd |
Compute the standard deviation along the specified axis, while ignoring NaNs. |
Function | nansum |
Return the sum of array elements over a given axis treating Not a Numbers (NaNs) as zero. |
Function | nanvar |
Compute the variance along the specified axis, while ignoring NaNs. |
Replace values in a
with NaN where mask
is True. This differs from
copyto in that it will deal with the case where a
is a numpy scalar.
a
is
replaced by val
. Broadcasts.val
.Compute a/b ignoring invalid results. If a
is an array the division
is done in place. If a
is a scalar, then its type is preserved in the
output. If out is None, then then a is used instead so that the
division is in place. Note that this is only called with a
an inexact
type.
a
was an ndarray the division is done
in place. If a
is a numpy scalar, the division preserves its type.Undocumented
Undocumented
Undocumented
sort + indexing median, faster for small medians along multiple dimensions due to the high overhead of apply_along_axis
see nanmedian for parameter usage
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Undocumented
Equivalent to arr1d[~arr1d.isnan()], but in a different order
Presumably faster as it incurs fewer copies
arr1d
can be modified in placeres
can be modified in place, given the constraint on the
inputIf a
is of inexact type, make a copy of a
, replace NaNs with
the val
value, and return the copy together with a boolean mask
marking the locations where NaNs were present. If a
is not of
inexact type, do nothing and return a
together with a mask of None.
Note that scalars will end up as array scalars, which is important for using the result as the value of the out argument in some operations.
a
is of inexact type, return a copy of a
with the NaNs
replaced by the fill value, otherwise return a
.a
is of inexact type, return a boolean mask marking locations of
NaNs, otherwise return None.Return the indices of the maximum values in the specified axis ignoring NaNs. For all-NaN slices ValueError is raised. Warning: the results cannot be trusted if a slice contains only NaNs and -Infs.
If provided, the result will be inserted into this array. It should be of the appropriate shape and dtype.
If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the array.
argmax, nanargmin
>>> a = np.array([[np.nan, 4], [2, 3]]) >>> np.argmax(a) 0 >>> np.nanargmax(a) 1 >>> np.nanargmax(a, axis=0) array([1, 0]) >>> np.nanargmax(a, axis=1) array([1, 1])
Return the indices of the minimum values in the specified axis ignoring NaNs. For all-NaN slices ValueError is raised. Warning: the results cannot be trusted if a slice contains only NaNs and Infs.
If provided, the result will be inserted into this array. It should be of the appropriate shape and dtype.
If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the array.
argmin, nanargmax
>>> a = np.array([[np.nan, 4], [2, 3]]) >>> np.argmin(a) 0 >>> np.nanargmin(a) 2 >>> np.nanargmin(a, axis=0) array([1, 1]) >>> np.nanargmin(a, axis=1) array([1, 0])
Return the cumulative product of array elements over a given axis treating Not a Numbers (NaNs) as one. The cumulative product does not change when NaNs are encountered and leading NaNs are replaced by ones.
Ones are returned for slices that are all-NaN or empty.
a
, unless a
has an integer dtype with
a precision less than that of the default platform integer. In
that case, the default platform integer is used instead.out
is
specified, in which case it is returned.numpy.cumprod : Cumulative product across array propagating NaNs. isnan : Show which elements are NaN.
>>> np.nancumprod(1) array([1]) >>> np.nancumprod([1]) array([1]) >>> np.nancumprod([1, np.nan]) array([1., 1.]) >>> a = np.array([[1, 2], [3, np.nan]]) >>> np.nancumprod(a) array([1., 2., 6., 6.]) >>> np.nancumprod(a, axis=0) array([[1., 2.], [3., 2.]]) >>> np.nancumprod(a, axis=1) array([[1., 2.], [3., 3.]])
Return the cumulative sum of array elements over a given axis treating Not a Numbers (NaNs) as zero. The cumulative sum does not change when NaNs are encountered and leading NaNs are replaced by zeros.
Zeros are returned for slices that are all-NaN or empty.
dtype
is not specified, it defaults
to the dtype of a
, unless a
has an integer dtype with a
precision less than that of the default platform integer. In
that case, the default platform integer is used.out
is
specified, in which it is returned. The result has the same
size as a
, and the same shape as a
if axis
is not None
or a
is a 1-d array.numpy.cumsum : Cumulative sum across array propagating NaNs. isnan : Show which elements are NaN.
>>> np.nancumsum(1) array([1]) >>> np.nancumsum([1]) array([1]) >>> np.nancumsum([1, np.nan]) array([1., 1.]) >>> a = np.array([[1, 2], [3, np.nan]]) >>> np.nancumsum(a) array([1., 3., 6., 6.]) >>> np.nancumsum(a, axis=0) array([[1., 2.], [4., 2.]]) >>> np.nancumsum(a, axis=1) array([[1., 3.], [3., 3.]])
Return the maximum of an array or maximum along an axis, ignoring any NaNs. When all-NaN slices are encountered a RuntimeWarning is raised and NaN is returned for that slice.
a
is not an
array, a conversion is attempted.Alternate output array in which to place the result. The default is None; if provided, it must have the same shape as the expected output, but the type will be cast if necessary. See :ref:`ufuncs-output-type` for more details.
If this is set to True, the axes which are reduced are left
in the result as dimensions with size one. With this option,
the result will broadcast correctly against the original a
.
If the value is anything but the default, then
keepdims
will be passed through to the max
method
of sub-classes of ndarray
. If the sub-classes methods
does not implement keepdims
any exceptions will be raised.
The minimum value of an output element. Must be present to allow
computation on empty slice. See ~numpy.ufunc.reduce
for details.
Elements to compare for the maximum. See ~numpy.ufunc.reduce
for details.
a
, with the specified axis removed.
If a
is a 0-d array, or if axis is None, an ndarray scalar is
returned. The same dtype as a
is returned.amin, fmin, minimum
NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic (IEEE 754). This means that Not a Number is not equivalent to infinity. Positive infinity is treated as a very large number and negative infinity is treated as a very small (i.e. negative) number.
If the input has a integer type the function is equivalent to np.max.
>>> a = np.array([[1, 2], [3, np.nan]]) >>> np.nanmax(a) 3.0 >>> np.nanmax(a, axis=0) array([3., 2.]) >>> np.nanmax(a, axis=1) array([2., 3.])
When positive infinity and negative infinity are present:
>>> np.nanmax([1, 2, np.nan, np.NINF]) 2.0 >>> np.nanmax([1, 2, np.nan, np.inf]) inf
Compute the arithmetic mean along the specified axis, ignoring NaNs.
Returns the average of the array elements. The average is taken over
the flattened array by default, otherwise over the specified axis.
float64
intermediate and return values are used for integer inputs.
For all-NaN slices, NaN is returned and a RuntimeWarning
is raised.
a
is not an
array, a conversion is attempted.float64
; for inexact inputs, it is the same as the input
dtype.If this is set to True, the axes which are reduced are left
in the result as dimensions with size one. With this option,
the result will broadcast correctly against the original a
.
If the value is anything but the default, then
keepdims
will be passed through to the mean
or sum
methods
of sub-classes of ndarray
. If the sub-classes methods
does not implement keepdims
any exceptions will be raised.
Elements to include in the mean. See ~numpy.ufunc.reduce
for details.
out=None
, returns a new array containing the mean values,
otherwise a reference to the output array is returned. Nan is
returned for slices that contain only NaNs.average : Weighted average mean : Arithmetic mean taken while not ignoring NaNs var, nanvar
The arithmetic mean is the sum of the non-NaN elements along the axis divided by the number of non-NaN elements.
Note that for floating-point input, the mean is computed using the same
precision the input has. Depending on the input data, this can cause
the results to be inaccurate, especially for float32
. Specifying a
higher-precision accumulator using the dtype
keyword can alleviate
this issue.
>>> a = np.array([[1, np.nan], [3, 4]]) >>> np.nanmean(a) 2.6666666666666665 >>> np.nanmean(a, axis=0) array([2., 4.]) >>> np.nanmean(a, axis=1) array([1., 3.5]) # may vary
Compute the median along the specified axis, while ignoring NaNs.
Returns the median of the array elements.
a
for
calculations. The input array will be modified by the call to
median
. This will save memory when you do not need to preserve
the contents of the input array. Treat the input as undefined,
but it will probably be fully or partially sorted. Default is
False. If overwrite_input
is True and a
is not already an
ndarray
, an error will be raised.If this is set to True, the axes which are reduced are left
in the result as dimensions with size one. With this option,
the result will broadcast correctly against the original a
.
If this is anything but the default value it will be passed
through (in the special case of an empty array) to the
mean
function of the underlying array. If the array is
a sub-class and mean
does not have the kwarg keepdims
this
will raise a RuntimeError.
out
is specified, that array is
returned instead.mean, median, percentile
Given a vector V of length N, the median of V is the middle value of a sorted copy of V, V_sorted - i.e., V_sorted[(N-1)/2], when N is odd and the average of the two middle values of V_sorted when N is even.
>>> a = np.array([[10.0, 7, 4], [3, 2, 1]]) >>> a[0, 1] = np.nan >>> a array([[10., nan, 4.], [ 3., 2., 1.]]) >>> np.median(a) nan >>> np.nanmedian(a) 3.0 >>> np.nanmedian(a, axis=0) array([6.5, 2. , 2.5]) >>> np.median(a, axis=1) array([nan, 2.]) >>> b = a.copy() >>> np.nanmedian(b, axis=1, overwrite_input=True) array([7., 2.]) >>> assert not np.all(a==b) >>> b = a.copy() >>> np.nanmedian(b, axis=None, overwrite_input=True) 3.0 >>> assert not np.all(a==b)
Return minimum of an array or minimum along an axis, ignoring any NaNs. When all-NaN slices are encountered a RuntimeWarning is raised and Nan is returned for that slice.
a
is not an
array, a conversion is attempted.Alternate output array in which to place the result. The default is None; if provided, it must have the same shape as the expected output, but the type will be cast if necessary. See :ref:`ufuncs-output-type` for more details.
If this is set to True, the axes which are reduced are left
in the result as dimensions with size one. With this option,
the result will broadcast correctly against the original a
.
If the value is anything but the default, then
keepdims
will be passed through to the min
method
of sub-classes of ndarray
. If the sub-classes methods
does not implement keepdims
any exceptions will be raised.
The maximum value of an output element. Must be present to allow
computation on empty slice. See ~numpy.ufunc.reduce
for details.
Elements to compare for the minimum. See ~numpy.ufunc.reduce
for details.
a
, with the specified axis
removed. If a
is a 0-d array, or if axis is None, an ndarray
scalar is returned. The same dtype as a
is returned.amax, fmax, maximum
NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic (IEEE 754). This means that Not a Number is not equivalent to infinity. Positive infinity is treated as a very large number and negative infinity is treated as a very small (i.e. negative) number.
If the input has a integer type the function is equivalent to np.min.
>>> a = np.array([[1, 2], [3, np.nan]]) >>> np.nanmin(a) 1.0 >>> np.nanmin(a, axis=0) array([1., 2.]) >>> np.nanmin(a, axis=1) array([1., 3.])
When positive infinity and negative infinity are present:
>>> np.nanmin([1, 2, np.nan, np.inf]) 1.0 >>> np.nanmin([1, 2, np.nan, np.NINF]) -inf
Compute the qth percentile of the data along the specified axis, while ignoring nan values.
Returns the qth percentile(s) of the array elements.
a
to be modified by
intermediate calculations, to save memory. In this case, the
contents of the input a
after this function completes is
undefined.This parameter specifies the method to use for estimating the percentile. There are many different methods, some unique to NumPy. See the notes for explanation. The options sorted by their R type as summarized in the H&F paper [1] are:
The first three methods are discontiuous. NumPy further defines the following discontinuous variations of the default 'linear' (7.) option:
If this is set to True, the axes which are reduced are left in
the result as dimensions with size one. With this option, the
result will broadcast correctly against the original array a
.
If this is anything but the default value it will be passed
through (in the special case of an empty array) to the
mean
function of the underlying array. If the array is
a sub-class and mean
does not have the kwarg keepdims
this
will raise a RuntimeError.
Deprecated name for the method keyword argument.
q
is a single percentile and axis=None
, then the result
is a scalar. If multiple percentiles are given, first axis of
the result corresponds to the percentiles. The other axes are
the axes that remain after the reduction of a
. If the input
contains integers or floats smaller than float64, the output
data-type is float64. Otherwise, the output data-type is the
same as that of the input. If out
is specified, that array is
returned instead.nanmean nanmedian : equivalent to nanpercentile(..., 50) percentile, median, mean nanquantile : equivalent to nanpercentile, except q in range [0, 1].
For more information please see numpy.percentile
>>> a = np.array([[10., 7., 4.], [3., 2., 1.]]) >>> a[0][1] = np.nan >>> a array([[10., nan, 4.], [ 3., 2., 1.]]) >>> np.percentile(a, 50) nan >>> np.nanpercentile(a, 50) 3.0 >>> np.nanpercentile(a, 50, axis=0) array([6.5, 2. , 2.5]) >>> np.nanpercentile(a, 50, axis=1, keepdims=True) array([[7.], [2.]]) >>> m = np.nanpercentile(a, 50, axis=0) >>> out = np.zeros_like(m) >>> np.nanpercentile(a, 50, axis=0, out=out) array([6.5, 2. , 2.5]) >>> m array([6.5, 2. , 2.5])
>>> b = a.copy() >>> np.nanpercentile(b, 50, axis=1, overwrite_input=True) array([7., 2.]) >>> assert not np.all(a==b)
[1] | R. J. Hyndman and Y. Fan, "Sample quantiles in statistical packages," The American Statistician, 50(4), pp. 361-365, 1996 |
Return the product of array elements over a given axis treating Not a Numbers (NaNs) as ones.
One is returned for slices that are all-NaN or empty.
a
is not an
array, a conversion is attempted.a
is used. An
exception is when a
has an integer type with less precision than
the platform (u)intp. In that case, the default will be either
(u)int32 or (u)int64 depending on whether the platform is 32 or 64
bits. For inexact inputs, dtype must be inexact.arr
.The starting value for this product. See ~numpy.ufunc.reduce
for details.
Elements to include in the product. See ~numpy.ufunc.reduce
for details.
out
is
specified, in which case it is returned.numpy.prod : Product across array propagating NaNs. isnan : Show which elements are NaN.
>>> np.nanprod(1) 1 >>> np.nanprod([1]) 1 >>> np.nanprod([1, np.nan]) 1.0 >>> a = np.array([[1, 2], [3, np.nan]]) >>> np.nanprod(a) 6.0 >>> np.nanprod(a, axis=0) array([3., 2.])
Compute the qth quantile of the data along the specified axis, while ignoring nan values. Returns the qth quantile(s) of the array elements.
a
to be modified by intermediate
calculations, to save memory. In this case, the contents of the input
a
after this function completes is undefined.This parameter specifies the method to use for estimating the quantile. There are many different methods, some unique to NumPy. See the notes for explanation. The options sorted by their R type as summarized in the H&F paper [1] are:
The first three methods are discontiuous. NumPy further defines the following discontinuous variations of the default 'linear' (7.) option:
If this is set to True, the axes which are reduced are left in
the result as dimensions with size one. With this option, the
result will broadcast correctly against the original array a
.
If this is anything but the default value it will be passed
through (in the special case of an empty array) to the
mean
function of the underlying array. If the array is
a sub-class and mean
does not have the kwarg keepdims
this
will raise a RuntimeError.
Deprecated name for the method keyword argument.
q
is a single percentile and axis=None
, then the result
is a scalar. If multiple quantiles are given, first axis of
the result corresponds to the quantiles. The other axes are
the axes that remain after the reduction of a
. If the input
contains integers or floats smaller than float64, the output
data-type is float64. Otherwise, the output data-type is the
same as that of the input. If out
is specified, that array is
returned instead.quantile nanmean, nanmedian nanmedian : equivalent to nanquantile(..., 0.5) nanpercentile : same as nanquantile, but with q in the range [0, 100].
For more information please see numpy.quantile
>>> a = np.array([[10., 7., 4.], [3., 2., 1.]]) >>> a[0][1] = np.nan >>> a array([[10., nan, 4.], [ 3., 2., 1.]]) >>> np.quantile(a, 0.5) nan >>> np.nanquantile(a, 0.5) 3.0 >>> np.nanquantile(a, 0.5, axis=0) array([6.5, 2. , 2.5]) >>> np.nanquantile(a, 0.5, axis=1, keepdims=True) array([[7.], [2.]]) >>> m = np.nanquantile(a, 0.5, axis=0) >>> out = np.zeros_like(m) >>> np.nanquantile(a, 0.5, axis=0, out=out) array([6.5, 2. , 2.5]) >>> m array([6.5, 2. , 2.5]) >>> b = a.copy() >>> np.nanquantile(b, 0.5, axis=1, overwrite_input=True) array([7., 2.]) >>> assert not np.all(a==b)
[1] | R. J. Hyndman and Y. Fan, "Sample quantiles in statistical packages," The American Statistician, 50(4), pp. 361-365, 1996 |
Compute the standard deviation along the specified axis, while ignoring NaNs.
Returns the standard deviation, a measure of the spread of a distribution, of the non-NaN array elements. The standard deviation is computed for the flattened array by default, otherwise over the specified axis.
For all-NaN slices or slices with zero degrees of freedom, NaN is
returned and a RuntimeWarning
is raised.
ddof
is zero.If this is set to True, the axes which are reduced are left
in the result as dimensions with size one. With this option,
the result will broadcast correctly against the original a
.
If this value is anything but the default it is passed through
as-is to the relevant functions of the sub-classes. If these
functions do not have a keepdims
kwarg, a RuntimeError will
be raised.
Elements to include in the standard deviation.
See ~numpy.ufunc.reduce
for details.
out
is None, return a new array containing the standard
deviation, otherwise return a reference to the output array. 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.var, mean, std nanvar, nanmean :ref:`ufuncs-output-type`
The standard deviation is the square root of the average of the squared deviations from the mean: std = sqrt(mean(abs(x - x.mean())**2)).
The average squared deviation 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 the infinite population. ddof=0 provides a maximum
likelihood estimate of the variance for normally distributed variables.
The standard deviation computed in this function is the square root of
the estimated variance, so even with ddof=1, it will not be an
unbiased estimate of the standard deviation per se.
Note that, for complex numbers, std
takes the absolute value before
squaring, so that the result is always real and nonnegative.
For floating-point input, the std is computed using the same
precision the input has. Depending on the input data, this can cause
the results to be inaccurate, especially for float32 (see example
below). Specifying a higher-accuracy accumulator using the dtype
keyword can alleviate this issue.
>>> a = np.array([[1, np.nan], [3, 4]]) >>> np.nanstd(a) 1.247219128924647 >>> np.nanstd(a, axis=0) array([1., 0.]) >>> np.nanstd(a, axis=1) array([0., 0.5]) # may vary
Return the sum of array elements over a given axis treating Not a Numbers (NaNs) as zero.
In NumPy versions <= 1.9.0 Nan is returned for slices that are all-NaN or empty. In later versions zero is returned.
a
is not an
array, a conversion is attempted.The type of the returned array and of the accumulator in which the
elements are summed. By default, the dtype of a
is used. An
exception is when a
has an integer type with less precision than
the platform (u)intp. In that case, the default will be either
(u)int32 or (u)int64 depending on whether the platform is 32 or 64
bits. For inexact inputs, dtype must be inexact.
Alternate output array in which to place the result. The default is None. If provided, it must have the same shape as the expected output, but the type will be cast if necessary. See :ref:`ufuncs-output-type` for more details. The casting of NaN to integer can yield unexpected results.
If this is set to True, the axes which are reduced are left
in the result as dimensions with size one. With this option,
the result will broadcast correctly against the original a
.
If the value is anything but the default, then
keepdims
will be passed through to the mean
or sum
methods
of sub-classes of ndarray
. If the sub-classes methods
does not implement keepdims
any exceptions will be raised.
Starting value for the sum. See ~numpy.ufunc.reduce
for details.
Elements to include in the sum. See ~numpy.ufunc.reduce
for details.
out
is
specified, in which it is returned. The result has the same
size as a
, and the same shape as a
if axis
is not None
or a
is a 1-d array.numpy.sum : Sum across array propagating NaNs. isnan : Show which elements are NaN. isfinite : Show which elements are not NaN or +/-inf.
If both positive and negative infinity are present, the sum will be Not A Number (NaN).
>>> np.nansum(1) 1 >>> np.nansum([1]) 1 >>> np.nansum([1, np.nan]) 1.0 >>> a = np.array([[1, 1], [1, np.nan]]) >>> np.nansum(a) 3.0 >>> np.nansum(a, axis=0) array([2., 1.]) >>> np.nansum([1, np.nan, np.inf]) inf >>> np.nansum([1, np.nan, np.NINF]) -inf >>> from numpy.testing import suppress_warnings >>> with suppress_warnings() as sup: ... sup.filter(RuntimeWarning) ... np.nansum([1, np.nan, np.inf, -np.inf]) # both +/- infinity present nan
Compute the variance along the specified axis, while ignoring NaNs.
Returns the variance of the array elements, a measure of the spread of a distribution. The variance is computed for the flattened array by default, otherwise over the specified axis.
For all-NaN slices or slices with zero degrees of freedom, NaN is
returned and a RuntimeWarning
is raised.
a
is not an
array, a conversion is attempted.float64
; for arrays of float types it is the same as
the array type.ddof
is zero.a
.Elements to include in the variance. See ~numpy.ufunc.reduce
for
details.
out
is None, return a new array containing the variance,
otherwise return a reference to the output array. 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.std : Standard deviation mean : Average var : Variance while not ignoring NaNs nanstd, nanmean :ref:`ufuncs-output-type`
The variance is the average of the squared deviations from the mean, i.e., 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 nonnegative.
For floating-point input, the variance is computed using the same
precision the input has. Depending on the input data, this can cause
the results to be inaccurate, especially for float32
(see example
below). Specifying a higher-accuracy accumulator using the dtype
keyword can alleviate this issue.
For this function to work on sub-classes of ndarray, they must define
sum
with the kwarg keepdims
>>> a = np.array([[1, np.nan], [3, 4]]) >>> np.nanvar(a) 1.5555555555555554 >>> np.nanvar(a, axis=0) array([1., 0.]) >>> np.nanvar(a, axis=1) array([0., 0.25]) # may vary