[Numpy-discussion] return index of maximum value in an array easily?

Chao YUE chaoyuejoy@gmail....
Fri Jan 11 17:26:13 CST 2013

```Hi,

np.nonzero(a==np.max(a)) as a workaround.

For the second point, in case I have an array:
a = np.arange(24.).reshape(2,3,4)

suppose I want to find the index for maximum value of each 2X3 array along
the 3rd dimension, what I can think of will be:

index_list = []
for i in range(a.shape[-1]):
data = a[...,i]
index_list.append(np.nonzero(data==np.max(data)))

In [87]:

index_list

Out[87]:

[(array([1]), array([2])),
(array([1]), array([2])),
(array([1]), array([2])),
(array([1]), array([2]))]

If we want to make the np.argmax function doing the job of this part of
code,
could we add another some kind of boolean keyword argument, for example,
"exclude" to the function?
[this is only my thinking, and I am only a beginner, maybe it's stupid!!!]

np.argmax(a,axis=2,exclude=True) (default value for exclude is False)

it will give the index of maximum value along all other axis except the
axis=2
(which is acutally the 3rd axis)

The output will be:

np.array(index_list).squeeze()

array([[1, 2],
[1, 2],
[1, 2],
[1, 2]])

and one can use a[1,2,i] (i=1,2,3,4) to extract the maximum value.

I doubt this is really useful...... too complicated......

Chao

On Fri, Jan 11, 2013 at 11:00 PM, Nathaniel Smith <njs@pobox.com> wrote:

> On Thu, Jan 10, 2013 at 9:40 AM, Chao YUE <chaoyuejoy@gmail.com> wrote:
> > Dear all,
> >
> > Are we going to consider returning the index of maximum value in an array
> > easily
> > without calling np.argmax and np.unravel_index consecutively?
>
> This does seem like a good thing to support somehow. What would a good
> interface look like? Something like np.nonzero(a == np.max(a))? Should
> we support vectorized operation (e.g. argmax of each 2-d subarray of a
> 3-d array along some axis)?
>
> -n
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>

--
***********************************************************************************
Chao YUE
Laboratoire des Sciences du Climat et de l'Environnement (LSCE-IPSL)
UMR 1572 CEA-CNRS-UVSQ
Batiment 712 - Pe 119
91191 GIF Sur YVETTE Cedex
Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16
************************************************************************************
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20130112/5bcf2610/attachment.html
```