[Numpy-discussion] numpy.ma.compress

Stefan van der Walt stefan at sun.ac.za
Thu Jan 24 15:58:14 EST 2008


Hi Pierre

On Thu, Jan 24, 2008 at 04:58:04AM -0500, Pierre GM wrote:
> On Thursday 24 January 2008 04:02:52 Stefan van der Walt wrote:
> > I'm not 100% sure about the new behaviour -- compress now removes
> > masked elements, instead of ignoring them.  Whereas a person would
> > have been able to do
> > compress(x,condition).compressed()
> > before, the mask information is now thrown away.
> 
> Mmh, OK, I see. The problem arises when the condition is masked. Filling w/ 
> False will get rid of the masked values, filling with True won't work either 
> (cf example below). An option is then to simply take the condition as a 
> ndarray.

How about masking the output where the condition is masked?
I.e. keep where condition is True, remove where condition is False and
mask where condition is masked.

> > The numpy docstring states that compress should be equivalent to
> > a[condition], which is no longer the case.
> Good point... In most cases, as long as an axis is not specified. Note that 
> the equivalence a.compress(condition) and a[condition] is not strictly true 
> even for regular ndarrays: take a look at the example on the scipy site 
> (http://www.scipy.org/Numpy_Example_List_With_Doc)
> 
> >>>b = array([[10,20,30],[40,50,60]])
> >>>b.compress(b.ravel() >= 22)
> array([30, 40, 50, 60])
> >>>b[b.ravel()>=22]
> IndexError: index (2) out of range (0<=index<=1) in dimension 0

You're right, better equivalent code would be

a.flat[condition]

> Anyhow, I just commited an update fixing our initial problem (viz, forcing 
> condition to a regular ndarray).

Thanks! I'll check it out.

Regards
Stéfan



More information about the NumPy-Discussion mailing list