[Numpy-discussion] fill_value in masked arrays

Pierre GM pgmdevlist at gmail.com
Sun Feb 11 13:14:37 EST 2007


On Saturday 10 February 2007 11:25:44 Keith Goodman wrote:
> The doc strings for MaskedArray and ma.argsort do not seem consistent.
numpy.core.ma ? Or the other implementation in the SVN ?

> The doc string for MaskedArray says "The fill_value is not used for
> computation within this module." But the doc string for ma.argsort
> says "Treating masked values as if they have the value fill_value,

The fill_value is used to convert a MaskedArray to a regular ndarray. When 
sorting an array with missing data, the fill_value is used to find the end at 
which the missing data should be: for example, when sorting a 1D float array, 
a fill_value of -inf will put the missing data on the left (beginning, small 
indices), a fill_value of +inf will put the missing data on the right (end, 
large indices). 
Other than that, the fill_value is not used in computations: for sum, it is 
temporarily set to 0, for prod, to 1...

> I'm trying to argsort matrices that have missing values.

You have an argsort method and an argsort function that behaves similarly. The 
output is an array of indices. Depending on the fill_value you chose, the 
indices corresponding to the missing data wil be either at the beginning or 
the end.

example:
>>>x=array([0,2,4,3,1],mask=[0,0,1,1,0])
>>>x
masked_array(data = [0 2 -- -- 1],
      mask = [False False  True  True False],
      fill_value=999999)

>>>x.argsort(fill_value=-999999)
array([2, 3, 0, 4, 1])
>>>x.argsort(fill_value=+999999)
array([0, 4, 1, 2, 3])

Now, if you want only the indices that are not masked, you can use something 
like that:
>>>a=x.argsort(fill_value=+99999)
>>>a[~x._mask[a]]
array([0,4,1])




More information about the NumPy-Discussion mailing list