Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What could cause numpy.nanstd() to return nan?

I have a relatively large 1-D array (20000 observations). When I calculate descrpitive statistics such as standard deviation, mean, sum, I get nan value, but when I calculate a percintle from the same array I get the value I expected. I tried functions such as numpy.nanstd, nanmean. What could I be doing wrong? I am using python 2.7.9

like image 868
kim Avatar asked Mar 10 '15 00:03

kim


People also ask

Why does Python return NaN?

Nan means “Not a number”, this is because inside your cube function, you're not calling the square function, but getting it's contents. Change return x * square; with return x * square(x); and it should work.

Why does mean return NaN?

Remarks. A method or operator returns NaN when the result of an operation is undefined.

How do I get rid of NP NaN?

Droping the missing values or nan values can be done by using the function "numpy. isnan()" it will give us the indexes which are having nan values and when combined with other function which is "numpy. logical_not()" where the boolean values will be reversed.

What is NP Nanstd?

nanstd() function compute the standard deviation along the specified axis, while ignoring NaNs. Syntax : numpy.nanstd(arr, axis = None, dtype = None, out = None, ddof = 0, keepdims) Parameters : arr : [array_like] Calculate the standard deviation of the non-NaN values.


1 Answers

There are three circumstances where np.nanstd might return NaN:

  1. If the input is empty

  2. If all of the elements in the input are NaN

  3. If one of the elements is either positive or negative infinity. To understand why this happens, remember that the formula for standard deviation is

    enter image description here

    Since x contains inf, the mean of x will also be inf. Therefore when computing the deviations from the mean, there is at least one element that is equal to inf - inf. If you try this at the IPython prompt, you will see that inf - inf is defined as NaN.

In the former two cases you should get a helpful warning:

RuntimeWarning: Degrees of freedom <= 0 for slice.
like image 105
ali_m Avatar answered Oct 14 '22 18:10

ali_m