[Numpy-discussion] Medians that ignore values

Pierre GM pgmdevlist@gmail....
Thu Sep 18 11:48:51 CDT 2008


On Thursday 18 September 2008 10:59:12 Peter Saffrey wrote:
> I had looked at masked arrays, but couldn't quite get them to work.

That's unfortunate.

>  >>> from numeric import *

Mmh, typo?

>  >>> from pylab import rand
>  >>> a = rand(10,3)
>  >>> a[a > 0.8] = nan
>  >>> m = ma.masked_array(a, isnan(a))
>  >>> m

Another way would be m = ma.masked_where(a>0.8,a)

> Remember I want medians of each triple, so I need to median the
> transposed matrix:
>  >>> median(m.T)

Ohoh. What version of numpy are you using ? if you don't give an axis 
parameter, you should get the median of the flattened array, therefore a 
scalar, not an array.
Anyway: you should use ma.median for masked arrays. Else, you're just keeping 
the NaNs where they were.


More information about the Numpy-discussion mailing list