Global minimum location
Locate the smallest element in a vector or multidimensional array.
NumPy · Function
Returns the indices of minimum values in an array, either globally or along a chosen axis. It can preserve reduced dimensions and optionally write into a supplied output array.
np.argmin returns integer positions of minimum elements. With axis=None it searches the flattened input; with an axis it reduces that dimension, and keepdims=True retains it with size one. When equal minima occur, it selects the first occurrence.
numpy.argmin(a, axis=None, out=None, *, keepdims=<no value>)| Name | Type | Description |
|---|---|---|
a | array_like | Input array. |
axis | int, optional | Reduction axis. By default, indexing is performed into the flattened array; otherwise it is performed along the specified axis. |
out | array, optional | Destination array into which the result is inserted; it must have the appropriate shape and dtype. |
keepdims | bool, optional | Whether reduced axes remain in the result with length one. |
index_array: ndarray of ints. Indices into the input array; its shape matches a.shape with the reduction axis removed, or retains that axis at size one when keepdims=True.
import numpy as np
a = np.array([[4, 1, 7], [2, 9, 0]])
print(np.argmin(a))
print(np.argmin(a, axis=0))
print(np.argmin(a, axis=1, keepdims=True))The default searches the flattened array; specifying an axis returns minimum positions along that axis, while keepdims preserves the reduced dimension.
Locate the smallest element in a vector or multidimensional array.
Find the best or lowest-valued option independently across rows or columns.
Keep reduction results shape-compatible with the source for later broadcasting.
It returns an integer array containing the positions of minimum values. With axis=None, the position refers to the flattened array; with an axis, the reduction happens independently along that axis.
Follow-up: How can the result retain the reduced dimension? Set keepdims=True, which leaves that axis present with length one.
If several elements share the minimum value, np.argmin returns the index associated with the first occurrence. This applies to the selected search domain, whether flattened or along an axis.
Follow-up: How would you obtain the minimum value itself? Use np.min or the array's minimum operation rather than np.argmin.
keepdims=True leaves each reduced axis in the output with size one. This makes the result shape compatible for broadcasting against the original array.
Follow-up: What must be true when using out? The destination must have the appropriate shape and dtype for the result.
numpy.ndarray.argminnumpy.argmaxnumpy.amin