[Numpy-discussion] np.ma.argmax not respecting the mask?

Sebastian Berg sebastian at sipsolutions.net
Tue Jul 9 10:08:04 EDT 2013


On Tue, 2013-07-09 at 15:14 +0200, Stéfan van der Walt wrote:
> On Tue, Jul 9, 2013 at 2:55 PM, Chao YUE <chaoyuejoy at gmail.com> wrote:
> > I am using 1.7.1 version of numpy and np.ma.argmax is not repecting the
> > mask?
> >
> > In [96]: d3
> > Out[96]:
> > masked_array(data =
> >  [[-- -- -- -- 4]
> >  [5 -- 7 8 9]],
> >              mask =
> >  [[ True  True  True  True False]
> >  [False  True False False False]],
> >        fill_value = 6)
> >
> >
> > In [97]: np.ma.argmax(d3,axis=0)
> > Out[97]: array([1, 0, 1, 1, 1])
> 
> This is the result I would expect.  If both values are masked, the
> fill value is used, so there is always an argmin value.
> 

To be honest, I would expect the exact opposite. If there is no value,
there is no minimum argument -> either its an error, or it signals
invalid in some other way. On masked arrays I would expect it to be
masked to signal this.
The error for nanargmax is annoying, but it is right to be an error IMO,
due to lack of a better representation. (Ideally mabe the user would be
given the option to pass an Identity element for those nanfuncs
(basically this is always NaN now, which fails for argmax since the
result is integer) for which the ufunc does not have an Identity, and
for those that do, we should actually use it.

- Sebastian

> The following workaround should have done the trick, but it exposes a
> different bug:
> 
> x = np.ma.array([[0,1,2,3,4],[5,6,7,8, 9]], mask=[[1, 1, 1, 1, 0], [0,
> 1, 0, 0 ,0]], dtype=float)
> np.nanargmax(x.filled(np.nan), axis=0)
> 
> This breaks with "ValueError: cannot convert float NaN to integer"
> 
> Stéfan
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
> 





More information about the NumPy-Discussion mailing list