.argmin()
Published Apr 25, 2025
Contribute to Docs
The .argmin()
function in NumPy returns the index of the smallest value within a NumPy array. It functions across both one-dimensional and multi-dimensional arrays. By specifying an axis, the function locates the index of the minimum value along that dimension.
This method is often utilized in computational tasks and data analysis to determine the position of the minimum element in an array.
Syntax
numpy.argmin(a, axis=None, out=None, keepdims=<no value>)
Parameters:
a
: Input array containing values for evaluation.axis
(Optional): Axis along which to find the indices of minimum values. IfNone
(default), the array is flattened before computation.out
(Optional): If provided, the result will be inserted into this array. It must have the appropriate shape to hold the output.keepdims
(Optional): If set toTrue
, the reduced axes are kept in the result as dimensions with size one, which maintains the original number of dimensions. Useful for broadcasting.
Return value:
- Without an
axis
specified, returns a scalar index of the smallest value in the flattened array. - With an axis specified, returns an array with indices of the minimum values along that axis.
Example
This example showcases how .argmin()
identifies the index of the smallest element in different configurations:
import numpy as nparr = np.array([[6,3,8],[5,2,9]])print(np.argmin(arr))# Minimum in flattened arrayprint(np.argmin(arr, axis=0))# Minimum indices along columnsprint(np.argmin(arr, axis=1))# Minimum indices along rows
The output produced by the exammple code will be:
4[1 1 0][1 1]
Codebyte Example
In this codebyte example, a 2D array of random integers is generated, followed by various applications of .argmin()
:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly95 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours