🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.argmin()

AI & DATA SCIENCE // np-argmin

np.argmin() returns the index of the smallest value in an array, rather than the value itself.

Syntax

np.argmin(arr, axis=None)

Deep Dive Course

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.

editor.html
import numpy as np

arr = np.array([5, 2, 8, 1, 9])
print(np.argmin(arr))
localhost:3000

2Practical Example

Here is a real-world application of np.argmin() showing how it is used in production NumPy code.

editor.html
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)
localhost:3000

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.

editor.html
import numpy as np

arr = np.array([5, 2, 8, 1, 9])
print(np.argmin(arr))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

arr = np.array([5, 2, 8, 1, 9])
print(np.argmin(arr))
Example 02Advanced Example
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)

Best Practices

  • Use np.unravel_index() to convert a flat argmin()/argmax() index back into multi-dimensional coordinates for a 2D+ array
  • Specify the axis argument explicitly when you want the position of the minimum along a specific dimension rather than across the whole flattened array
  • Remember argmin() returns the first index on a tie — don't assume it will find every occurrence of the minimum value

Interview Question

Why does np.argmin() on a 2D array, without an axis argument, return a single integer instead of a (row, column) pair?

Hint: Think about what array argmin() is actually searching.

Without an axis argument, argmin() first conceptually flattens the array into one long 1D sequence and finds the index of the smallest value within that flattened sequence, returning that single flat index. It doesn't automatically know you'd want row/column coordinates instead, since a flat index is the simplest, most general result for an array of any dimensionality. To convert that flat index back into meaningful multi-dimensional coordinates, you pass it, along with the array's original shape, to np.unravel_index().

Exercises

MediumPractice using np.argmin() in a real scenario.
View Solution
import numpy as np

arr = np.array([5, 2, 8, 1, 9])
print(np.argmin(arr))

Frequently Asked Questions

Why does np.argmin() on a 2D array, without an axis argument, return a single integer instead of a (row, column) pair?

Without an axis argument, argmin() first conceptually flattens the array into one long 1D sequence and finds the index of the smallest value within that flattened sequence, returning that single flat index. It doesn't automatically know you'd want row/column coordinates instead, since a flat index is the simplest, most general result for an array of any dimensionality. To convert that flat index back into meaningful multi-dimensional coordinates, you pass it, along with the array's original shape, to np.unravel_index().

Related Functions

np-argmaxnp-minnp-where