Sub topic 9
Finding where the maximum or minimum is
This chapter assumes you can create arrays with np.array and np.arange, read an array's shape, and use the axis argument with sum, max, and min. Sometimes you need to know where the largest or smallest value sits, not what it is. argmax and argmin answer that question.
From values to positions
a.max(axis=0) returns the largest value in each column. a.argmax(axis=0) returns the index of that value within each column. The axis rule is the same as for sum: the chosen axis is collapsed, and the result loses that axis.
import numpy as np
a = np.array([[1, 2, 3],
[4, 5, 6]])
print(a.argmax(axis=0)) # [1 1 1]
print(a.argmax(axis=1)) # [2 2]With axis=0, NumPy compares rows down each column. In every column the second row (index 1) holds the larger value, so the result is [1 1 1]. With axis=1, NumPy compares columns across each row. In both rows the last column (index 2) is largest, so the result is [2 2].
The returned numbers are positions along the reduced axis. They are not row or column labels for the whole array.
Using argmin and the result shape
argmin works the same way and returns the position of the smallest value.
import numpy as np
a = np.array([[7, 2, 9],
[4, 8, 1]])
print(a.argmin(axis=0)) # [1 0 1]
print(a.argmin(axis=1)) # [1 2]
print(a.argmin(axis=1).shape) # (2,)The shape rule from the earlier chapters still holds. An array of shape (2, 3) reduced along axis=1 gives shape (2,), one index per row.
Without an axis: flat indices
If you omit axis, NumPy treats the array as if it were flattened into one line, in row-major order, and returns a single index into that line.
import numpy as np
a = np.array([[7, 2, 9],
[4, 8, 1]])
flat = a.argmax()
print(flat) # 2
print(np.unravel_index(flat, a.shape)) # (np.int64(0), np.int64(2))
print(a[0, 2]) # 9np.unravel_index converts the flat index back into a (row, column) pair. The exact printed form of the tuple depends on your NumPy version. Older versions print (0, 2), and NumPy 2 prints the np.int64 wrappers.
Getting the values back
To fetch the maximum in each row using the positions, pair row numbers with the column indices. This is called fancy indexing.
import numpy as np
a = np.array([[7, 2, 9],
[4, 8, 1]])
cols = a.argmax(axis=1)
rows = np.arange(a.shape[0])
print(a[rows, cols]) # [9 8]In practice a.max(axis=1) gives the same values. The index form is useful when you need the positions as well, for example to look up a label stored in a parallel array.
import numpy as np
names = np.array(["north", "south", "east"])
sales = np.array([[10, 40, 25],
[30, 20, 50]])
best = sales.argmax(axis=1)
print(names[best]) # ['south' 'east']Ties and common mistakes
- Ties: if the maximum appears more than once,
argmaxreturns the first occurrence only.np.array([3, 9, 9]).argmax()is1. - NaN values:
argmaxandargmintreat NaN as the extreme value, so a NaN in the data is returned as the position. Usenp.nanargmaxandnp.nanargminto skip missing values. - Wrong axis: if the result has the wrong length, check which axis you collapsed. For shape
(2, 3),axis=0gives 3 results andaxis=1gives 2. - Reading the result as a pair: with an axis given, you get one number per output element, not a
(row, column)pair.
Exercise
Given m = np.array([[3, 8, 1], [9, 2, 7]]), what do m.argmax(axis=0), m.argmin(axis=1), and m.argmax() return?
Show answer
m.argmax(axis=0) is [1 0 1], because the larger value in column 0 is 9 (row 1), in column 1 is 8 (row 0), and in column 2 is 7 (row 1). m.argmin(axis=1) is [2 1], because the smallest value in row 0 is 1 (column 2) and in row 1 is 2 (column 1). m.argmax() is 3, the flat index of 9 in the flattened array [3 8 1 9 2 7].
Exercise
A (4, 5) array holds scores for 4 students across 5 tests. Which call gives the test number of each student's best score, and what is the shape of its result?
Show answer
scores.argmax(axis=1). It collapses the test axis and returns one index per student, so the shape is (4,).