[Numpy-discussion] numpy.ma.compress

Pierre GM pgmdevlist@gmail....
Thu Jan 24 03:58:04 CST 2008


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.

>>>a=masked_array([1,2,3,4,5],mask=[0,0,1,1,0])
>>>a.compress(a<4)
masked_array(data = [1 2],
      mask = [False False],
      fill_value=999999)
>>>a.compress((a<4).filled(True))
masked_array(data = [1 2 -- --],
      mask = [False False  True  True],
      fill_value=999999)
>>>a.compress((a<4).view(ndarray))
masked_array(data = [1 2 --],
      mask = [False False  True],
      fill_value=999999)
>>>a[a<4]
masked_array(data = [1 2 --],
      mask = [False False  True],
      fill_value=999999)

> 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

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


More information about the Numpy-discussion mailing list