[Numpy-discussion] question on NumPy NaN
Tue May 20 22:31:43 CDT 2008
On Tue, May 20, 2008 at 6:12 PM, David Cournapeau
> Keith Goodman wrote:
>> np.nansum(a) / np.isfinite(a).sum()
>> A nanmean would be nice to have in numpy.
> nanmean, nanstd and nanmedian are available in scipy, though.
Thanks for pointing that out. Studying nanmedian, which is twice as
fast as my for-loop implementation, taught me about compress and
>> import numpy.matlib as mp
>> from numpy.matlib import where
>> timeit x[0, where(x.A > 0.5)]
10000 loops, best of 3: 60.8 µs per loop
>> timeit x.compress(x.A.ravel() > 0.5)
10000 loops, best of 3: 44.5 µs per loop
Am I missing something obvious or is 'sort' unnecessary in _nanmedian?
Perhaps it is left over from a time when _nanmedian did not call
def _nanmedian(arr1d): # This only works on 1d arrays
"""Private function for rank a arrays. Compute the median ignoring Nan.
arr1d : rank 1 ndarray
m : float
cond = 1-np.isnan(arr1d)
x = np.sort(np.compress(cond,arr1d,axis=-1))
if x.size == 0:
More information about the Numpy-discussion