
Get the index of the max value in an array in NumPy
Getting the position of the max value in a NumPy array:
import numpy as np
a = np.array([5, 6, 7, 4])
b = np.array([3, 2, 1])
i = np.argmax(a)
j = np.argmax(b)
print(i) # 2
print(j) # 0
argmax()
returns the index of the max value. In fact, a[2]
and b[0]
are max values respectively in a
and b
. If the array has maximum values at multiple positions, argmax()
returns the first index of that.
import numpy as np
a = np.array([5, 6, 7, 7, 4])
i = np.argmax(a)
print(i) # 2
max()
NumPy has the max()
that is confusing with argmax()
but both are totally different.
import numpy as np
a = np.array([5, 6, 7, 7, 4])
i = np.max(a)
print(i) # 7
The max()
returns the maximum value of an NumPy array.
Comments
Powered by Markdown