[Numpy-discussion] argmax

Ed Schofield schofield at ftw.at
Wed May 10 02:25:20 EDT 2006


Tim Hochberg wrote:
>     >>> m
>    matrix([[0, 1, 2],
>           [3, 4, 5],
>           [6, 7, 8]])
>     >>> m.argmax()
>    matrix([[8]])
>
> Does anyone else think that this is a fairly nonsensical result? Not
> that this is specific to matrices, the array result is just as weird:
>
>     >>> a
>    array([[0, 1, 2],
>           [3, 4, 5],
>           [6, 7, 8]])
>     >>> a.argmax()
>    8
>
> Given that obj[obj.argmax()] should really equal obj.max(), argmax
> with no axis specified should really either raise an exception or
> return a tuple.

I came across this last week, and I came to a similar conclusion.  I
agree that a sequence of indices would be far more useful.  This
sequence could be either an array or a tuple:

With a tuple:

>>> a
array([[0, 1, 2],
          [3, 4, 5],
          [6, 7, 8]])
>>> a.argmax()
(2, 2)
>>> a[a.argmax()] == a.max()
True
>>> b = array([0, 10, 20])
>>> b.argmax()
(2,)

With an array:

>>> a
array([[0, 1, 2],
          [3, 4, 5],
          [6, 7, 8]])
>>> a.argmax()
array([(2,2)]
>>> a[tuple(a.argmax())] == a.max()
True
>>> b = array([0, 10, 20])
>>> print b.argmax()
2
>>> type(b.argmax())
<type 'numpy.ndarray'>
[currently <type 'int32scalar'>]


I think either one would be more useful than the current
ravel().argmax() behaviour.  A tuple would be more consistent with the
nonzero method.

-- Ed





More information about the NumPy-Discussion mailing list