[Numpy-discussion] Using Reduce with Multi-dimensional Masked array

Sue Giller sag at hydrosphere.com
Wed Nov 28 09:02:05 CST 2001


I posted the following inquiry to python-list at python.org   earlier this 
week, but got no responses, so I thought I'd try a more focused 
group.  I assume MA module falls under NumPy area.

I am using 2 (and more) dimensional masked arrays with some 
numeric data, and using the reduce functionality on the arrays.  I 
use the masking because some of the values in the arrays are 
'missing' and should not be included in the results of the reduction.

For example, assume a 5 x 2 array, with masked values for the 4th 
entry for both of the 2nd dimension cells.  If I want to sum along the 
2nd dimension, I would expect to get a 'missing' value for the 4th 
entry because both of the entries for the sum are 'missing'.  Instead, 
I get 0, which might be a valid number in my data space, and the 
returned 1 dimensional array has no mask associated with it.

Is this expected behavior for masked arrays or a bug or am I 
misusing the mask concept?  Does anyone know how to get the 
reduction to produce a masked value?

Example Code:
>>> import MA
>>> a = MA.array([[1,2,3,-99,5],[10,20,30,-99,50]])
>>> a
   [[  1,  2,  3,-99,  5,]
    [ 10, 20, 30,-99, 50,]]
>>> m = MA.masked_values(a, -99)
>>> m
    array(data = 
             [[  1,  2,  3,-99,  5,]
              [ 10, 20, 30,-99, 50,]],
           mask = 
              [[0,0,0,1,0,]
               [0,0,0,1,0,]],
           fill_value=-99)

>>> r = MA.sum(m)
>>> r
      array([11,22,33, 0,55,])
>>> t = MA.getmask(r)
>>> print t
      None





More information about the Numpy-discussion mailing list