0

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

3 Answers 3

2

Use argmax that returns the indices of the maximum values along an axis:

np.argmax(a)

3

As you don't supply the axis it will return the index of flattened array:

a = np.array([[1, 2, 3, 4], [2, 3, 3, 9]])

np.argmax(a)

7
Sign up to request clarification or add additional context in comments.

2 Comments

The index 7 is not suitable I think. We can not use it like a[7]. Why the return is like this?
@ToughMind As stated, it looks at array in it's flattened version and returns the index max. Also, the return value will always be the 1st occurrence.
2

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])

Comments

-1

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))

2 Comments

This returns the max value, OP wants index.
sorry. fixed my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.