argmax() and argmin() (NumPy)
np.argmax() returns the index of the largest element in an array and np.argmin() returns the index of the smallest. They give you the position of the extreme value rather than the value itself.
NumPy
Often you want to know where the maximum is, not just what it is — which class a model scored highest, which day had peak traffic. argmax answers that with an index you can then use to look up related data. On a flat array it returns a single integer position.
With an axis argument they work per row or per column: argmax(axis=1) gives the index of the max in each row. If several elements tie for the extreme, both functions return the index of the first occurrence.
To get the value as well, index back into the array: a[a.argmax()]. The related np.argsort returns the indices that would sort the whole array, generalizing the same idea.
Syntax
np.argmax(a, axis=None) | np.argmin(a, axis=None)Examples
Python — Index 2 holds the largest value, 7.
import numpy as np
a = np.array([3, 1, 7, 2])
print(a.argmax())Output:
2