Numpy argmin – Python NumPy argmin() Function

Numpy argmin: Python provides us with a powerful module that allows us to work on difficult mathematical operations.

The NumPy module, which is also noted for its high level of complexity, provides us with a variety of data structures to work with. We frequently deal with data in terms of Arrays and Matrices. Analytics and preprocessing appear to be inadequate without NumPy as the mathematical model solution, whether it is a computation problem or a real-life problem.

NumPy argmin() Function:

Python argmin: The argmin() function of the NumPy module returns the indices of the minimum values along an axis. By default, it is calculated over the flattened array; otherwise, it is calculated over the given axis.

Syntax:

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

Parameters

a: This is required. It is the array given as input

axis: This is optional. Indicates the axis or axes along which the indices of the minimum values are calculated. The flattened array is used by default to calculate it.

out: This is optional. It indicates the output array for the result. None is the default value. It must have the same shape as output if it is provided.

Return Value: 

Argmin python: When out=None, returns an array with the indices of the minimum values; otherwise, returns a reference to the output array.

NumPy argmin() Function in Python

Example1

Approach:

  • Import numpy module using the import keyword.
  • Pass some random size(rowsize*colsize) to the arange() function and apply the reshape() function by passing arguments rowsize and column size.
  • Print the given array.
  • Pass the given array as an argument to the argmin() function of numpy module to get the minimum index value in the entire given array.
  • Store it in another variable.
  • Print the minimum index value in the entire given array.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random size(rowsize*colsize) to the arange() function and apply the reshape() 
# function by passing arguments rowsize and column size
gvn_arr = np.arange(16).reshape(4,4) 
# Print the given array
print("The given array\n", gvn_arr)
# Pass the given array as an argument to the argmin() function of numpy module 
# to get the minimum index value in the entire given array 
# Store it in another variable.
min_indx = np.argmin(gvn_arr)
# Print the  minimum index value in the entire given array 
print("The minimum index value in the entire given array = ", min_indx)

Output:

The given array
[[ 0 1 2 3]
 [ 4 5 6 7]
 [ 8 9 10 11]
 [12 13 14 15]]
The minimum index value in the entire given array = 0

Example2

NP argmin: When the axis parameter is given, the index of minimum value over the defined axes can be determined.

Approach:

  • Import numpy module using the import keyword.
  • Pass some random size(rowsize*colsize) to the arange() function and apply the reshape() function by passing arguments rowsize and column size.
  • Print the given array.
  • Pass the given array, axis=0 as the arguments to the argmin() function of numpy module to get the minimum index value in the entire given array along axis=0.
  • Store it in another variable.
  • Print the minimum index value in the entire given array along axis=0.
  • Pass the given array, axis=1 as the arguments to the argmin() function of numpy module to get the minimum index value in the entire given array along axis=1.
  • Store it in another variable.
  • Print the minimum index value in the entire given array along axis=1.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random size(rowsize*colsize) to the arange() function and apply the reshape() 
# function by passing arguments rowsize and column size
gvn_arry = np.arange(16).reshape(4,4) 
# Print the given array
print("The given array\n", gvn_arry)             
# Pass the given array, axis=0 as the arguments to the argmin() function of numpy module 
# to get the minimum index value in the entire given array along axis=0
# Store it in another variable.
rslt_1 = np.argmin(gvn_arry, axis=0)
# Print the minimum index value in the entire given array along axis=0
print("The minimum index value in the entire given array along axis=0: ", rslt_1)
# Pass the given array, axis=1 as the arguments to the argmin() function of numpy module 
# to get the minimum index value in the entire given array along axis=1
# Store it in another variable.
rslt_2 = np.argmin(gvn_arry, axis=1)
# Print the minimum index value in the entire given array along axis=1
print("The minimum index value in the entire given array along axis=1: ", rslt_2)

Output:

The given array
[[ 0 1 2 3]
 [ 4 5 6 7]
 [ 8 9 10 11]
 [12 13 14 15]]
The minimum index value in the entire given array along axis=0: [0 0 0 0]
The minimum index value in the entire given array along axis=1: [0 0 0 0]

Example3

The function returns the index of the first occurrence of the minimal value when the array contains more than one.

# Import numpy module using the import keyword
import numpy as np
# Pass some random list as an argument to the array() function to
# create an array. 
# Store it in a variable.
gvn_arr = np.array([10, 5, 55, 200])              
# Print the given array
print("The given array\n", gvn_arr)
# Pass the given array as an argument to the argmin() function of numpy module 
# to get the minimum index value in the entire given array 
# Store it in another variable.
min_indx = np.argmin(gvn_arr)
# Print the  minimum index value in the entire given array 
print("The minimum index value in the entire given array = ", min_indx)

Output:

The given array
[ 10 5 55 200]
The minimum index value in the entire given array = 1