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

2013-01-11 Thread Nathaniel Smith
On Thu, Jan 10, 2013 at 9:40 AM, Chao YUE chaoyue...@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


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

2013-01-11 Thread Sebastian Berg
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 n...@pobox.com
 wrote:
 On Thu, Jan 10, 2013 at 9:40 AM, Chao YUE
 chaoyue...@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


___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


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

2013-01-10 Thread Hanno Klemm

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
kl...@phys.ethz.ch
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion