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

Hanno Klemm klemm@phys.ethz...
Thu Jan 10 04:00:57 CST 2013


Hi Chao,

in two dimensions the following works very well:

In [97]: a = np.random.randn(5,7)

In [98]: a[divmod(a.argmax(), a.shape[1])]
Out[98]: 1.3680204597100922

In [99]: a.max()
Out[99]: 1.3680204597100922

In [100]:

In [100]: b = a[divmod(a.argmax(), a.shape[1])]

In [101]: b==a.max()
Out[101]: True

Cheers,
Hanno


On 10.01.2013 10:40, Chao YUE 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?
>
> I saw few posts in mailing archive and stackover flow on this, when I
> tried to return
>  the index of maximum value of 2d array.
>
> It seems that I am not the first to be confused by this.
>
> 
> http://stackoverflow.com/questions/11377028/getting-index-of-numpy-ndarray 
> [1]
>  
> http://old.nabble.com/maximum-value-and-corresponding-index-td24834930.html 
> [2]
> 
> http://stackoverflow.com/questions/5469286/how-to-get-the-index-of-a-maximum-element-in-a-numpy-array
> [3]
>
> 
> http://stackoverflow.com/questions/4150542/determine-index-of-highest-value-in-pythons-numpy
> [4]
>
> cheers,
>
> Chao
> --
>
> 
> ***********************************************************************************
>
> 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
>
> 
> ************************************************************************************
>
> Links:
> ------
> [1] 
> http://stackoverflow.com/questions/11377028/getting-index-of-numpy-ndarray
> [2] 
> http://old.nabble.com/maximum-value-and-corresponding-index-td24834930.html
> [3]
> 
> http://stackoverflow.com/questions/5469286/how-to-get-the-index-of-a-maximum-element-in-a-numpy-array
> [4]
> 
> http://stackoverflow.com/questions/4150542/determine-index-of-highest-value-in-pythons-numpy
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion

-- 
Hanno Klemm
klemm@phys.ethz.ch


More information about the NumPy-Discussion mailing list