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

Paul F. Dubois paul at pfdubois.com
Wed Nov 28 20:31:03 CST 2001


[dubois at ldorritt ~]$ pydoc MA.sum
Python Library Documentation: function sum in MA

sum(a, axis=0, fill_value=0)
    Sum of elements along a certain axis using fill_value for missing.

If you use add.reduce, you'll get what you want.
>>> print m
[[1 ,2 ,3 ,-- ,5 ,]
 [10 ,20 ,30 ,-- ,50 ,]]
>>> MA.sum(m)
array([11,22,33, 0,55,])
>>> MA.add.reduce(m)
array(data = 
 [ 11, 22, 33,-99, 55,],
      mask = 
 [0,0,0,1,0,],
      fill_value=-99)

In other words,
   sum(m, axis, fill_value) = add.reduce(filled(m, fill_value), axis)

Surprising in your case. Still, both uses are quite common, so I
probably was thinking to myself that since add.reduce already does one
of the jobs, I might as well make sum do the other one. One could have
just as well argued that one was a synonym for the other and so it is
revolting to have them be different.

Well, MA users, is this something I should change, or not?

-----Original Message-----
From: numpy-discussion-admin at lists.sourceforge.net
[mailto:numpy-discussion-admin at lists.sourceforge.net] On Behalf Of Sue
Giller
Sent: Wednesday, November 28, 2001 9:03 AM
To: numpy-discussion at lists.sourceforge.net
Subject: [Numpy-discussion] Using Reduce with Multi-dimensional Masked
array


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


_______________________________________________
Numpy-discussion mailing list Numpy-discussion at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/numpy-discussion





More information about the Numpy-discussion mailing list