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
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.
Remarks. A method or operator returns NaN when the result of an operation is undefined.
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.
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.
There are three circumstances where np.nanstd
might return NaN:
If the input is empty
If all of the elements in the input are NaN
If one of the elements is either positive or negative infinity. To understand why this happens, remember that the formula for standard deviation is
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With