Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does sum() operation on numpy masked_array change fill value to 1e20?

Tags:

python

numpy

Is this a feature or a bug? Can someone explain to me this behavior of a numpy masked_array? It seems to change the fill_value after applying the sum operation, which is confusing if you intend to use the filled result.

data=ones((5,5))
m=zeros((5,5),dtype=bool)

"""Mask out row 3"""
m[3,:]=True
arr=ma.masked_array(data,mask=m,fill_value=nan)

print arr
print 'Fill value:', arr.fill_value
print arr.filled()


farr=arr.sum(axis=1)
print farr
print 'Fill value:', farr.fill_value
print farr.filled()

"""I was expecting this"""
print nansum(arr.filled(),axis=1)

Prints output:

[[1.0 1.0 1.0 1.0 1.0]
 [1.0 1.0 1.0 1.0 1.0]
 [1.0 1.0 1.0 1.0 1.0]
 [-- -- -- -- --]
 [1.0 1.0 1.0 1.0 1.0]]
Fill value: nan
[[  1.   1.   1.   1.   1.]
 [  1.   1.   1.   1.   1.]
 [  1.   1.   1.   1.   1.]
 [ nan  nan  nan  nan  nan]
 [  1.   1.   1.   1.   1.]]
[5.0 5.0 5.0 -- 5.0]
Fill value: 1e+20
[  5.00000000e+00   5.00000000e+00   5.00000000e+00   1.00000000e+20
   5.00000000e+00]
[  5.   5.   5.  nan   5.]
like image 479
agartland Avatar asked Sep 18 '13 18:09

agartland


People also ask

What is fill value in masked array?

Numpy with Python The filling value of the masked array is a scalar. A masked array is the combination of a standard numpy. ndarray and a mask.

What is masked array Numpy?

A masked array is the combination of a standard numpy. ndarray and a mask. A mask is either nomask , indicating that no value of the associated array is invalid, or an array of booleans that determines for each element of the associated array whether the value is valid or not.


1 Answers

The array returned by arr.sum is a new array which does not inherit the fill_value of arr (though I agree that might be a nice improvement to np.ma). As a workaround, you could use

In [18]: farr.filled(arr.fill_value)
Out[18]: array([  5.,   5.,   5.,  nan,   5.])
like image 104
unutbu Avatar answered Nov 03 '22 13:11

unutbu