[Numpy-discussion] question on NumPy NaN

Keith Goodman kwgoodman@gmail....
Tue May 20 22:31:43 CDT 2008


On Tue, May 20, 2008 at 6:12 PM, David Cournapeau
<david@ar.media.kyoto-u.ac.jp> 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)


More information about the Numpy-discussion mailing list