[Numpy-discussion] Nansum function behavior
jni.soma at gmail.com
Sat Oct 24 02:08:18 EDT 2015
Just providing an outsider's perspective...
Your specific use-case doesn't address the general definition of nansum: perform a sum while ignoring nans. As others have pointed out, (especially in the linked thread) the sum of nothing is 0. Although the current behaviour of nansum doesn't quite match your use-case, there is no doubt at all that it follows a consistent convention. "Wrong" is certainly not the correct way to describe it.
You can easily cater to your use case as follows:
def rilhac_nansum(ar, axis=None):
if axis is None:
return np.nanmean(ar, axis=axis) * ar.shape[axis]
nanmean _consistently_ returns nans when encountering nan-only values because the mean of nothing is nan (the sum of nothing divided by the length of nothing, ie 0/0).
Hope this helps...
On Sat, Oct 24, 2015 at 12:44 PM, Charles Rilhac <webmastertux1 at gmail.com>
> I saw this thread and I totally disagree with thouis argument…
> Of course, you can have NaN if there are only NaNs. Thanks goodness, There is a lot of way to do that.
> But it’s not convenient, consistent and above all, it is wrong logically to do that. NaN does not mean zeros and operation with NaN only cannot return a figure…
> You lose information about your array. It is easier to fill the result of nansum with zeros than to keep a mask of your orignal array or whatever you do.
> Why it’s misleading ?
> For example you want to sum rows of a array and mean the result :
> a = np.array([[2,np.nan,4], [np.nan,np.nan, np.nan]])
> b = np.nansum(a, axis=1) # array([ 6., 0.])
> m = np.nanmean(b) # 3.0 WRONG because you wanted to get 6
>> On 24 Oct 2015, at 09:28, Stephan Hoyer <shoyer at gmail.com> wrote:
>> Hi Charles,
>> You should read the previous discussion about this issue on GitHub:
>> For what it's worth, I do think the new definition of nansum is more consistent.
>> If you want to preserve NaN if there are no non-NaN values, you can often calculate this desired quantity from nanmean, which does return NaN if there are only NaNs.
>> NumPy-Discussion mailing list
>> NumPy-Discussion at scipy.org
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the NumPy-Discussion