[Numpy-discussion] Medians that ignore values

jh@physics.uc... jh@physics.uc...
Thu Sep 18 09:32:21 CDT 2008


> You might want to try isfinite() to first remove nan, +/- infinity 
> before doing that.
> numpy.median(a[numpy.isfinite(a)])

We just had this discussion a month or two ago, I think even on this
list, and continued it at the SciPy conference.

The problem with

numpy.median(a[numpy.isfinite(a)])

is that it breaks when you have a multi-dimensional array, such as an
array of 5000x3 as in this case, and take median down an axis.  The
example above flattens the array and eliminates the possibility of
taking the median down an axis in a single call, as the poster desires.

Currently the only way you can handle NaNs is by using masked arrays.
Create a mask by doing isfinite(a), then call the masked array
median().  There's an example here:

http://sd-2116.dedibox.fr/pydocweb/doc/numpy.ma/

Note that our competitor language IDL does have a /nan flag to its
single median routine, making this common task much easier in that
language than ours.

--jh--


More information about the Numpy-discussion mailing list