[SciPy-user] How to use argmax ?
Stefan van der Walt
stefan at sun.ac.za
Tue Oct 23 02:28:22 EDT 2007
Hi,
You can access the maximum directly as follows:
x = N.array([[10,50,30],[60,20,40]])
p = x.argmin()
x.flat[p]
You can also convert 'p' to an index, using
N.unravel_index(p,x.shape)
Regards
Stéfan
On Mon, Oct 22, 2007 at 11:00:18AM -0700, LB wrote:
>
> 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