numpy.argmin() in Python


The Python numpy.argmin() function returns the indices of minimum elements along the specific axis inside the array.

Basic Syntax

Following is the basic syntax for numpy.argmin() function in Python:

numpy.argmin(arr, axis=None, out=None)

And the parameters are:

Parameter Description
arr The input array
axis [int, OPTIONAL] Along the axis like 1 or 0.
out [array, OPTIONAL] If provided then it will insert output to the out array with appropriate shape.

 

Return Value

This function returns array of indices into the array.

Example’s

Following are the examples for numpy.argmin() function:

Example 1

# Python Program illustration of numpy.argmin() function 
import numpy as np 
# Generating 2D array for input 
array = np.arange(15).reshape(3, 5) 
print("The input array: \n", array) 
# without axis
print("\nThe min element: ", np.argmin(array))
# with axis
print("\nThe indices of minimum element: ", np.argmin(array, axis=0)) 
print("\nThe indices of minimum element: ", np.argmin(array, axis=1))  

The output for the above program is as given below:

The input array: [
[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]]
The min element: 0
The indices of minimum element axis = 0: [0, 0, 0, 0, 0]
The indices of minimum element axis = 1: [0, 0, 0]

Example 2

# Python Program illustration of numpy.argmin() function 
import numpy as np 
# Generating 2D array for input 
array = np.arange(20).reshape(4, 5) 
array[0][2] = 17
print("The input array: \n", array) 
# without axis
print("\nThe min element: ", np.argmin(array))
# with axis
print("\nThe indices of minimum element: ", np.argmin(array, axis=0)) 
print("\nThe indices of minimum element: ", np.argmin(array, axis=1)) 

The output for the above program is as given below:

The input array:
[[ 0, 1, 17, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]]
The min element: 0
The indices of minimum element axis = 0: [0, 0, 1, 0, 0]
The indices of minimum element axix = 1: [0, 0, 0, 0]