dimanche 12 juin 2016

argmax with masked arrays


I stumbled over a strange fact concerning masked unsigned integer arrays and np.ma.argmax.

Consider the following array:

>>> marr = np.ma.array(np.array([[2,2,2], [3,3,3], [1,1,1]]), mask=False, dtype=np.uint16)
>>> marr
masked_array(data =
 [[2 2 2]
 [3 3 3]
 [1 1 1]],
             mask =
 [[False False False]
 [False False False]
 [False False False]],
       fill_value = 999999)

If I use np.ma.argmax the result is what I expected:

>>> print(np.ma.argmax(marr, axis=0))
[1 1 1]

If I now mask the last row the result is weird:

>>> marr.mask[2] = True
>>> marr
masked_array(data =
 [[2 2 2]
 [3 3 3]
 [-- -- --]],
             mask =
 [[False False False]
 [False False False]
 [ True  True  True]],
       fill_value = 999999)

>>> print(marr.argmax(axis=0))
[1 1 1]
>>> print(np.argmax(marr, axis=0))
[1 1 1]
>>> print(np.ma.argmax(marr, axis=0))
[2 2 2]   # why?

It now thinks the masked row is the maximum? I even changed the fill_value to 0 but the result stays the same: It still thinks the masked row is the maximum. It seems like this only affects unsigned integer arrays.

Why is the np.ma.argmax not doing the correct thing here? As far as I can see it is defined as the method itself.


Aucun commentaire:

Enregistrer un commentaire