Python

NumPy np.argmin()

The Python NumPy package provides us with the argmin() function, which allows us to get the index of the min element in an array at a particular axis.

Let us discuss.

NumPy Argmin Function Syntax

The function provides a minimalistic syntax as shown:

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

Parameters

The function parameters are as shown below:

  1. a – refers to the input array. This is a non-optional parameter.
  2. Axis – specifies along which axis to apply the argmin() function. If set to None, the function will flatten the array and use the function on all elements.
  3. Out – specifies an alternative output array. The output array must have the same shape as the output value.
  4. Keepdims – a Boolean value that allows you to preserve the axes reduced in the result as dimensions with a size of one.

Function Result

The function will return an array of indices with the same shape as a.shape and the dimensions along the specified axis removed.

Example 1

The following is an example that illustrates how to use the argmin() function with a 1D array in Python.

# import numpy
import numpy as np
arr = np.array([1,2,3,4,5,6,7,8])
print(f"index of min element -> {np.argmin(arr)}")

In the code above, we have a 1D array holding elements from 1 to 8. We then check the minimum element in the array using the argmin() function and return its index.

The output is as shown:

index of min element -> 0

Example 2

Let us see what happens when applying the same operation on a 2D array.

arr_2d = np.array([[[1,2,3,4], [5,6,7,8]]])
print(f"index of min element -> {np.argmin(arr_2d)}")

In the code above, we apply the argmin() function to a 2D array without specifying the axis. This flattens the array and applies the function.

The resulting value is as shown:

index of min element -> 0

Example 3

To act along a specific axis, we can set the axis parameter as shown:

arr_2d = np.array([[[1,2,3,4], [5,6,7,8]]])
print(f"indices of min elements -> {np.argmin(arr_2d, axis=0)}")

The code above should apply the argmin() function along axis 0 and return the indices of the min elements as shown in the output array:

indices of min elements -> [[0 0 0 0]
[0 0 0 0]]

Example 4

To apply the function on the last axis, we can set the axis value as -1 as shown below:

arr_2d = np.array([[[1,2,3,4], [5,6,7,8]]])
print(f"indices of min elements -> {np.argmin(arr_2d, axis=-1)}")

The code above should return:

indices of min elements -> [[0 0]]

Conclusion

Throughout this article, we explored the NumPy argmin function, its syntax, parameters, and return values. We also provided various examples illustrating how the function works in multiple scenarios.

Happy coding!!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list