How to find index of max value in Numpy?

Let’s learn how to find the index of the maximum value in a Numpy array using the argmax function.

Numpy index of max value

How to use numpy index of max?

You may need to find the index of the max value in the Numpy array. The argmax function can be used to find the index of the maximum value in a Numpy array.

See also  How to sum up array in Numpy?

Argmax function scans the whole array and finds the first instance of the max value. It returns the index of the first value.

import numpy as np

my_list = np.array([5, 7, 8, 3, 2, 8])
print(f"This is my array: \n {my_list}")

max_value = my_list.argmax()
print(f"Index of max value: \n {max_value}")

As you may see, there are two occurrences of 8, but argmax returned the index of the first one.

See also  Correcting AxisError: axis x is out of bounds for array of dimension y