NumPy | argmax method
argmax(~) method returns the index that corresponds to the largest element in the array.
If your array has missing values (i.e.
NaNs), then the
np.argmax(~) method will consider them as the largest value. If you want to ignore missing values, then use the
np.nanargmax(~) method instead.
The input array.
The axis along which to compute the method. For 2D arrays, if
axis=0, then the method is performed column-wise, and if
axis=1 then row-wise. If no axis is provided, then NumPy will deem your array as a flattened array.
axis is provided, then a scalar is returned. Otherwise, a NumPy array is returned.
x = np.array([3,5,2,1])np.argmax(x)1
Here, 1 is returned because the largest value (i.e. 5) is located at index 1.
Suppose we have the following 2D NumPy array:
x = np.array([[1,5],[2,3]])xarray([[1, 5],[2, 3]])
Max index of entire array
To obtain the index of the maximum value in the entire array, leave out the
Max indices of every column
To obtain the index of the maximum values column-wise, set
np.argmax(x, axis=0)array([1, 0])
Here, we're going over each column of the matrix and computing the index of its largest value.
Max indices of every row
To obtain the index of the maximum values row-wise, set
np.argmax(x, axis=1)array([1, 1])
Here, we're going over each row of the matrix and computing the index of its largest value.