On Tue, May 20, 2008 at 6:12 PM, David Cournapeau

<

[hidden email]> wrote:

> Keith Goodman wrote:

>> Or

>>

>> 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

apply_along_axis.

>> import numpy.matlib as mp

>> from numpy.matlib import where

>> timeit x[0, where(x.A > 0.5)[1]]

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

median.

def _nanmedian(arr1d): # This only works on 1d arrays

"""Private function for rank a arrays. Compute the median ignoring Nan.

:Parameters:

arr1d : rank 1 ndarray

input array

:Results:

m : float

the median."""

cond = 1-np.isnan(arr1d)

x = np.sort(np.compress(cond,arr1d,axis=-1))

if x.size == 0:

return np.nan

return median(x)

_______________________________________________

Numpy-discussion mailing list

[hidden email]
http://projects.scipy.org/mailman/listinfo/numpy-discussion