[Numpy-discussion] Medians that ignore values

Bruce Southey bsouthey@gmail....
Thu Sep 18 08:28:45 CDT 2008


Nadav Horesh wrote:
> I think you need to use masked arrays.
>
>   Nadav
>
>
> -----הודעה מקורית-----
> מאת: numpy-discussion-bounces@scipy.org בשם Peter Saffrey
> נשלח: ה 18-ספטמבר-08 14:27
> אל: numpy-discussion@scipy.org
> נושא: [Numpy-discussion] Medians that ignore values
>  
> I have data from biological experiments that is represented as a list of 
> about 5000 triples. I would like to convert this to a list of the median 
> of each triple. I did some profiling and found that numpy was much about 
> 12 times faster for this application than using regular Python lists and 
> a list median implementation. I'll be performing quite a few 
> mathematical operations on these values, so using numpy arrays seems 
> sensible.
>
> The only problem is that my data has gaps in it - where an experiment 
> failed, a "triple" will not have three values. Some will have 2, 1 or 
> even no values. To keep the arrays regular so that they can be used by 
> numpy, is there some dummy value I can use to fill these gaps that will 
> be ignored by the median routine?
>
> I tried NaN for this, but as far as median is concerned, it counts as 
> infinity:
>
>  >>> from numpy import *
>  >>> median(array([1,3,nan]))
> 3.0
>  >>> median(array([1,nan,nan]))
> nan
>
> Is this the correct behavior for median with nan? Is there a fix for 
> this or am I going to have to settle with using lists?
>
> Thanks,
>
> Peter
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
>   
> ------------------------------------------------------------------------
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>   
Hi,
The counting of infinity is correct due to the implementation of IEEE 
Standard for Binary Floating-Point for Arithmetic (IEEE 754).

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

Bruce


More information about the Numpy-discussion mailing list