Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy argmax. How to compute both max and argmax?

Tags:

python

numpy

Is there a way to get max and argmax by one stroke ?

import numpy as np
a=[0,0,1,0]
maximum=max(a)
index=np.argmax(a)

Is there a fastest way to do it, with something like:

[maximum,index]=function(a)
like image 243
PatriceG Avatar asked Nov 04 '14 15:11

PatriceG


People also ask

What is the difference between NP Max and NP argmax?

Essentially, the argmax function returns the index of the maximum value of a Numpy array. What is this? It's somewhat similar to the Numpy maximum function, but instead of returning the maximum value, it returns the index of the maximum value.

What does argmax () do in Python?

The argmax function returns the argument or arguments (arg) for the target function that returns the maximum (max) value from the target function.

Can argmax return multiple values?

argmax() function returns the indices of the maximum values along an axis. In case of multiple occurrences of the maximum values, the indices corresponding to the first occurrence will be returned.

How do you find the index of a maximum value in a Numpy array?

There is argmin() and argmax() provided by numpy that returns the index of the min and max of a numpy array respectively.


1 Answers

Maybe something like this is faster...

index = np.argmax(a)
max = a[index]
like image 73
PatriceG Avatar answered Oct 11 '22 00:10

PatriceG