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

Sebastian Berg sebastian@sipsolutions....
Fri Jan 11 19:33:07 CST 2013


On Sat, 2013-01-12 at 00:26 +0100, Chao YUE wrote:
> Hi,
> 
> I don't know how others think about this. Like you point out, one can
> use 
> 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)))
> 
To keep being close to min/max (and other ufunc based reduce
operations), it would seem consistent to allow something like
np.argmax(array, axis=(1, 2)), which would give a tuple of
arrays as result such that

array[np.argmax(array, axis=(1,2))] == np.max(array, axis=(1,2))

But apart from consistency, I am not sure anyone would get the idea of
giving multiple axes into the function...

> 
> 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
> 
> ************************************************************************************
> 
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion




More information about the NumPy-Discussion mailing list