[Numpy-discussion] max argmax combo

Charles R Harris charlesr.harris at gmail.com
Mon Sep 18 22:56:39 EDT 2006


On 9/18/06, Bill Baxter <wbaxter at gmail.com> wrote:
>
> I find myself often wanting both the max and the argmax of an array.
> (And same for the other arg* functions)
> Of course I can do argmax first then use fancy indexing to get the max as
> well.
> But the result of argmax isn't really in a format that's readily
> usable as an index.
> You have to do something like
>     a = rand(10,5)
>     imax = a.argmax(axis=0)
>     vmax = a[(imax, range(5))]
>
> Which isn't terrible, just always takes me a moment to remember the
> proper indexing expression.
> Would a way to get the argmax and the max at the same time be of
> interest to anyone else?  Maybe an extra 'ret' arg on argmax?
>
>    a = rand(10,5)
>    imax,vmax = a.argmax(axis=0,retmax=True)


I don't generally like overloading return values, the function starts to
lose its definition and becomes a bit baroque where simply changing a
keyword value can destroy the viability of the following code. But I can see
the utility of what you want. Hmm,  this problem is not unique to argmax.
Maybe what we need is a general way to extract values, something like

extract(a, imax, axis=0)

to go along with all the single axis functions.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20060918/9eb68fc6/attachment.html>


More information about the NumPy-Discussion mailing list