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

Sue Giller sag at hydrosphere.com
Thu Nov 29 09:49:02 EST 2001


Thanks for the pointer.

The example I gave using the sum operation is merely an example - 
I could also be doing other manipulations such as min, max, 
average, etc.

I see that the MA.<op>.reduce functions will do what I want, but to 
do an average, I will need to do two steps since the MA.average 
function will have the original 'unexpected' behavior that I don't want.

That raises the question of how to determine a count of valid values 
in a masked array.  Can I assume that I can do 'math' on the mask 
array itself, for example to sum along a given axis and have the 
masked cells add up?

In my original example, I would expect a sum along the second axis 
to return [0,0,0,2,0].  Can I rely on this?  I would suggest that a 
.count operator would be very useful in working with masked arrays 
(count valid and count masked).

>>> 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)

To add an opinion on the question from Paul about 'expected' 
behavior, I was working off the documentation for Numerical Python, 
and there were no caveats in there about MA.<op> working one 
way, and MA.<op>.reduce working another.  The answer is always 
in the documentation, especially for users like me who don't have 
time or knkowledge to go reading thru all the code modules to try 
and figure out what is happening.  From a purely user standpoint, I 
would expect a masked array to retain it's mask-edness at all times, 
unless I explicitly tell it not to.  In that case, I would still expect it to 
replace the 'masked' cells with the original masked value, and not 
just arbitrarily assign some other value, such as 0.

Thanks again for the prompt reply.





More information about the NumPy-Discussion mailing list