[SciPy-user] How to use argmax ?

LB berthe.loic at gmail.com
Mon Oct 22 14:00:18 EDT 2007


    Hi,

Let's say we have an array a :

If a is 1D, we have :
>>> a = array([1, 2, 5, 6,3, 4])
>>> ind = a.argmax()
>>> a[ind]
6
>>> a[ind] == a.max()
True

but if a is 2D (or more) :

a   = array([[10,50,30],[60,20,40]])

I can know where the maxima are with the argmax function :
ind = a.argmax(axis=0)
>>> a
array([[10, 50, 30],
       [60, 20, 40]])
>>> ind
array([1, 0, 1])

But what if a is 2D or more :
>>> a = array([[10, 20, 14], [60, 5, 7]])
>>> ind = a.argmax(axis=0)
>>> a
array([[10, 20, 14],
       [60,  5,  7]])
>>> ind
array([1, 0, 0])
>>> a[ind]
array([[60,  5,  7],
       [10, 20, 14],
       [10, 20, 14]])
>>> a.max(axis=0)
array([60, 20, 14])

How can I combine a and ind to access to the max ?

--
LB




More information about the SciPy-User mailing list