Where np.min() tells you what the smallest value is, np.argmin() tells you where it is — its index position. On a flattened or 1D array, it returns a single integer index; on a multi-dimensional array without specifying an axis, it returns the index into the flattened version of the array, which you'd need np.unravel_index() to convert back into multi-dimensional coordinates. If multiple elements tie for the minimum, argmin() returns the index of the first one encountered.
1Understanding np.argmin()
Where np.min() tells you what the smallest value is, np.argmin() tells you where it is — its index position. On a flattened or 1D array, it returns a single integer index; on a multi-dimensional array without specifying an axis, it returns the index into the flattened version of the array, which you'd need np.unravel_index() to convert back into multi-dimensional coordinates. If multiple elements tie for the minimum, argmin() returns the index of the first one encountered.
On a multi-dimensional array, np.argmin() without an axis argument returns an index into the flattened array, not a tuple of per-dimension coordinates — use np.unravel_index(np.argmin(arr), arr.shape) to convert it back to a row/column position.
import numpy as np
arr = np.array([5, 2, 8, 1, 9])
print(np.argmin(arr))2Practical Example
Here is a real-world application of np.argmin() showing how it is used in production NumPy code.
import numpy as np
matrix = np.array([[5, 2], [8, 1]])
flat_index = np.argmin(matrix)
coords = np.unravel_index(flat_index, matrix.shape)
print(flat_index, coords)3Best Practices
Follow these guidelines when working with np.argmin():
1. Use np.unravel_index() to convert a flat argmin()/argmax() index back into multi-dimensional coordinates for a 2D+ array
2. Specify the axis argument explicitly when you want the position of the minimum along a specific dimension rather than across the whole flattened array
3. Remember argmin() returns the first index on a tie — don't assume it will find every occurrence of the minimum value
Tip: On a multi-dimensional array, np.argmin() without an axis argument returns an index into the flattened array, not a tuple of per-dimension coordinates — use np.unravel_index(np.argmin(arr), arr.shape) to convert it back to a row/column position.
import numpy as np
arr = np.array([5, 2, 8, 1, 9])
print(np.argmin(arr))