Reputation: 477
I wish to get the element index that is the highest in the array.
import numpy as np
a = np.array([1,2,3,4])
print(np.where(a==a.max()))
Current output:
(array([3]),)
Expected output: 3
Upvotes: 0
Views: 65
Reputation: 930
You can use np.argmax()
. It will return the index of the highest value in your array.
For more deatils on the function here is a link to the documentation.
np.argmax()
also works for 2D-arrys:
a = array([[10, 11, 12],
[13, 14, 15]])
np.argmax(a)
>>> 5
np.argmax(a, axis=0)
>>> array([1, 1, 1])
np.argmax(a, axis=1)
>>> array([2, 2])
Upvotes: 2
Reputation: 87
Try this, it will return the value of the largest element in the array
import numpy as np
a = np.array([1,2,3,4])
print(np.argmax(a))
Upvotes: -1