New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
numpy.nanmean() does not skip nan±… or …±nan #59
Comments
Thanks. Strictly speaking, this is the expected behavior: Now, I will check whether there is any way to make NumPy understand that |
Wouldn't it be preferable to make |
The general idea of never producing
Now, I would have to think about |
First, athanks a lot for this extremely useful module! I have just been playing around with this, and discovered that if I convert all occurrences of So apparently, there is no way to do a |
Thanks! It is actually possible to a NaN-mean even when you are using uncertainties. With >>> import uncertainties as unc
>>> from uncertainties import unumpy
>>> import numpy as np
>>> nan = float("nan")
>>> arr = np.array([nan, unc.ufloat(nan, 1), unc.ufloat(1, nan), 2])
>>> arr
array([nan, nan+/-1.0, 1.0+/-nan, 2], dtype=object) you can get the NaN-mean by selecting only the values with a non-NaN nominal value: >>> arr[~unumpy.isnan(arr)].mean()
1.5+/-nan or more directly by asking NumPy to skip them: >>> np.ma.array(arr, mask=unumpy.isnan(arr))
masked_array(data=[--, --, 1.0+/-nan, 2],
mask=[ True, True, False, False],
fill_value='?',
dtype=object)
>>> _.mean()
1.5+/-nan In this case the uncertainty is NaN as it should be, because one of the numbers does have an undefined uncertainty, which makes the final uncertainty undefined (but not the average). In general, uncertainties are not NaN and you obtain the mean of the non-NaN values. (Edited so as to reflect the fact that the uncertainties module already provides |
PS: I added all the information (and more) from my post above to the documentation: http://uncertainties-python-package.readthedocs.io/en/latest/genindex.html#N. Thank you for your feedback! |
Hello!
First of all, great piece of work! It's saving me a lot of time :)
I'm having issues with
numpy.nanmean
that should ignorenan
values when calculating the mean.Here some test code:
Here the output:
From the output, you can see that both
mean
andnanmean
are returningnan+/-error
. I'd say that the later should return the mean ignoring thenan
values.I hope you can help with that!
Thanks
The text was updated successfully, but these errors were encountered: