Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Numpy argmin() Function



The Numpy argmin() function is used to find the index of the minimum value in an array along a specified axis. It is particularly useful for tasks that require identifying the position of the lowest value in data structures, such as arrays or matrices.

The Numpy argmin() function works on both one-dimensional and multi-dimensional arrays. When the function is used with multi-dimensional arrays, we need to specify the axis along which to find the minimum index.

In an array, if the minimum element appears multiple times, the numpy.argmin() function will return the index of the first occurrence of the minimum value.

Syntax

Following is the syntax of the Numpy argmin() function −

numpy.argmin(a, axis=None, out=None, keepdims=<no value>)

Parameters

Following are the parameters of the Numpy argmin() function −

  • a: The input array from which the minimum value index is to be found.
  • out (optional): If provided, the result will be placed in this output array. Must be of the same shape and type as the expected output.
  • keepdims: If set to True, the reduced axes are retained in the result as dimensions of size 1, meaning they are not removed. This ensures the result has the same number of dimensions as the original array, which is useful for broadcasting.
  • axis (optional): The axis along which to find the index of the minimum value.
  • None (default): Finds the index of the minimum value in the flattened array.
  • Axis can be an integer (e.g., 0 for rows, 1 for columns in a 2D array).

Return Type

This function returns an integer (for 1D arrays) or an array of integers (for multi-dimensional arrays), indicating the indices of the minimum values along the specified axis.

Example

Following is a basic example to find the index of the minimum value in a one-dimensional array using Numpy argmin() function −

import numpy as np
array = np.array([10, 20, 5, 30, 15])
min_index = np.argmin(array)
print("Array:", array)
print("Index of Minimum Value:", min_index)

Output

Array: [10 20  5 30 15]
Index of Minimum Value: 2

Example: Using axis Parameter

The axis parameter is used to find the minimum value indices along a specific axis in multi-dimensional arrays.

In the following example, we find the minimum element along rows and columns by setting the axis parameter to 1 (row-wise) and 0 (column-wise), respectively, in the numpy.argmin() function −

import numpy as np

array_2d = np.array([[85, 3, 7],
                     [2, 8, 6],
                     [66, 50, 9]])
# Finding min indices along rows
min_index_row = np.argmin(array_2d, axis=1)  
# Finding min indices along columns
min_index_col = np.argmin(array_2d, axis=0)  
print("2D Array:")
print(array_2d)
print("Indices of Minimum Values Along Rows:", min_index_row)
print("Indices of Minimum Values Along Columns:", min_index_col)

Output

2D Array:
[[85  3  7]
 [ 2  8  6]
 [66 50  9]]
Indices of Minimum Values Along Rows: [1 0 2]
Indices of Minimum Values Along Columns: [1 0 2]

Example: Flattened Array

When axis=None, the array is flattened, and the index of the minimum value in the flattened array is returned.

Here is an example where we find the minimum element in a 2D array by flattening it first −

import numpy as np

array_2d = np.array([[10, 20, 30],
                     [5, 25, 15],
                     [40, 35, 50]])

min_index_flat = np.argmin(array_2d)

print("2D Array:")
print(array_2d)
print("Index of Minimum Value in Flattened Array:", min_index_flat)

Output

2D Array:
[[10 20 30]
 [ 5 25 15]
 [40 35 50]]
Index of Minimum Value in Flattened Array: 3

Example: Using out Parameter

The out parameter can be used to store the result in a pre-allocated array. The output array must have the appropriate shape and data type.

import numpy as np

array = np.array([4, 12, 7, 18, 9, 0])
out_array = np.empty((), dtype=int)
np.argmin(array, out=out_array)

print("Array:", array)
print("Index of Minimum Value (using out):", out_array)

Output

Array: [ 4 12  7 18  9  0]
Index of Minimum Value (using out): 5

Example: Multiple Occurrence of Minimum Value

The Numpy argmin() function returns the index of the first occurrence of the minimum value if it appears multiple times in the array.

In the following example, the value 5 occurs twice, but the numpy.argmin() function returns the index of its first occurrence −

import numpy as np

array = np.array([4, 59, 2, 76, 19, 5, 2])
min_value = np.argmin(array)

print("Array:", array)
print("Index of Minimum Value:", min_value)

Output

Array: [ 4 59  2 76 19  5  2]
Index of Minimum Value: 2
numpy_array_manipulation.htm
Advertisements