argmin & argmax
Find WHERE the smallest or largest value is — which match was the best, which player scored most — with argmin, argmax and unravel_index.
max() tells you the highest score was 103. But the next question is always "in which match?" or "by which player?". For that you need the position of the maximum, not its value. That's what argmax (and its partner argmin) return.
Definition
argmaxreturns the index (position) of the largest value, andargminreturns the index of the smallest value. If the extreme value appears more than once, the index of the first occurrence is returned.
- "arg" — short for argument: the input position that produces the max, as in maths ("arg max").
- Index, not value —
argmaxgives6; you then usescores[6]to get the value 103. - First occurrence — if two matches are both ducks,
argminreturns the earlier match.
| You want | Use | Returns |
|---|---|---|
| the best score | a.max() | a value (103) |
| which match had the best score | a.argmax() | an index (6) |
| the worst score | a.min() | a value |
| which match had the worst score | a.argmin() | an index |
Syntax
python
np.argmax(a, axis=None, keepdims=False) a.argmax(axis=None)
np.argmin(a, axis=None, keepdims=False) a.argmin(axis=None)
np.unravel_index(flat_index, shape) # flat index -> (row, col)| Parameter | Meaning | Default |
|---|---|---|
a | input array | — required |
axis | None = index into the flattened array; 0/1 = one index per column/row | None |
keepdims | keep the reduced axis with size 1 | False |
flat_index (unravel_index) | an index into the flattened array | — required |
shape (unravel_index) | the shape of the original array | — required |
Example — best and worst match (1D)
python
import numpy as np
virat = np.array([72, 45, 88, 31, 64, 0, 103, 52, 19, 77])
best = virat.argmax()
worst = virat.argmin()
print("Best innings : index", best, "-> match", best + 1, "->", virat[best], "runs")
print("Worst innings : index", worst, "-> match", worst + 1, "->", virat[worst], "runs")text
Best innings : index 6 -> match 7 -> 103 runs
Worst innings : index 5 -> match 6 -> 0 runsHow it works: indexes start at 0, so index 6 is match 7. Adding + 1 turns an index into a human "match number". Using the index to look back into the array (virat[best]) gives the value.
Example — on 2D data with axis
python
import numpy as np
players = ["Virat", "Rohit", "Hardik", "Shubman"]
scores = np.array([
[72, 45, 88, 31, 64],
[56, 0, 74, 88, 32],
[45, 32, 0, 67, 18],
[33, 61, 45, 0, 77],
])
print("Best match of each player (axis=1):", scores.argmax(axis=1))
top = scores.argmax(axis=0)
print("Top scorer row per match (axis=0) :", top)
print("Top scorer names:", [players[i] for i in top])text
Best match of each player (axis=1): [2 3 3 4]
Top scorer row per match (axis=0) : [0 3 0 1 3]
Top scorer names: ['Virat', 'Shubman', 'Virat', 'Rohit', 'Shubman']How it works: argmax(axis=1) looks along each row and returns a column index (which match). argmax(axis=0) looks down each column and returns a row index (which player), which we map to names.
Example — no axis on 2D: a flat index
Without axis, NumPy flattens the 2D array first, so the answer is a single flat index. Convert it back to (row, column) with np.unravel_index:
python
import numpy as np
scores = np.array([
[72, 45, 88, 31, 64],
[56, 0, 74, 88, 32],
[45, 32, 0, 67, 18],
[33, 61, 45, 0, 77],
])
flat = scores.argmax()
print("flat index:", flat)
row, col = np.unravel_index(flat, scores.shape)
print("row:", int(row), "col:", int(col), "value:", scores[row, col])text
flat index: 2
row: 0 col: 2 value: 88How it works: the maximum 88 appears twice (Virat match 3 and Rohit match 4). In the flattened array Virat's 88 is at position 2, which comes first, so argmax returns 2 → row 0, column 2.
Common mistakes
- ❌ Expecting
argmaxto return the value. ✅ It returns a position; usea[a.argmax()]ora.max()for the value. - ❌ Using
scores.argmax()on a 2D array and treating it as a row number. ✅ It is a flat index — usenp.unravel_indexor passaxis. - ❌ Forgetting it is 0-based ("match 6" when it is really match 7). ✅ Add 1 for human numbering.
- ❌ Assuming ties return all positions. ✅ Only the first is returned; use
np.flatnonzero(a == a.max())ornp.where(a == a.max())to get all of them. - ❌ Arrays with
NaN:argmaxreturns the position of the NaN. ✅ Usenp.nanargmax(see NaN-safe functions).
Interview tip
Q: How do you find the row and column of the maximum value in a 2D array?
A: np.unravel_index(a.argmax(), a.shape). argmax() without axis returns an index into the flattened array, and unravel_index converts it back to a (row, col) tuple.
Key points to remember
- argmax / argmin return the INDEX of the largest / smallest value, not the value itself.
- With ties, only the first occurrence's index is returned.
- Add 1 to a 0-based index to get a human match number.
- On 2D data without axis, argmax returns a flat index — convert with np.unravel_index(i, a.shape).
- argmax(axis=1) gives one column index per row; argmax(axis=0) gives one row index per column.
- Use the returned indexes to look up labels, e.g. players[stds.argmin()].
Try it yourself
Best and worst match
Top scorer of each match
Row and column of the overall max
Practice: worst match of each player
Revision Flashcards
Tap a card to flip it and see the answer.
