Python Argmin
Introduction
The argmin function is defined as numpy.argmin(). This function returns the index of the minimum value or element from a Numpy array in a specific axis. An array is taken as the input by this function and gives output as the minimum element index.
The input array can be specified as a single-dimensional array or a multi-dimensional array.
By using argmin function we can also do array sorting.
About Numpy
The Numpy is a package in Python which is generally used for processing arrays.
The Numpy can be easily installed via pip with the following command
pip install numpy
Import Numpy
It can easily import with the Import keyword
Syntax:
import numpy as np
Argmin installation
Argmin cab be installed via PIP with below command.
pip install argmin
Syntax of Numpy argmin:
numpy.argmin(a, axis=None, out=None)
Parameters :
array: It is the input array for which the minimum element index has to be determined.
axis: This is an optional parameter that takes an integer value. By default, It is none if the value is not defined.
out: If this parameter is defined, then the result provided by the argmin function is stored in this array. The size of the array is specified in a manner by which it is appropriate with the returned shape of the array.
Return: index of the minimum value or element from a Numpy array in a specific axis.
Example:
import numpy as np
# Working on 1D array
array = np.arange(8)
print("INPUT ARRAY : \n", array)
# This returns minimum minimum element indices
print("\nIndex of min element : ", np.argmin(array, axis=0))
Output:
[0 1 2 3 4 5 6 7 8 9]
Index of min element : 0
For multidimensional array
The argmin() function can also be used for calculate the minimum value for a multidimensional array. The result will be computed with the axis of the multidimensional array.
Example:
import numpy as np
# Working on 2D array
array = np.random.randint(16, size=(4, 4))
print("INPUT ARRAY : \n", array)
print("\nIndices of min element : ", np.argmin(array, axis = 0))
Output:
INPUT ARRAY :
[[ 9 11 10 0]
[10 6 0 11]
[ 6 4 15 2]
[12 12 10 1]]
Indices of min element : [2 2 1 0]
With the different axis values
Now, we are trying to change the axis values of the argmin function to check how the output will be changed.
Here, we will take axis =1.
Example:
import numpy as np
# Working on 2D array
array = np.random.randint(16, size=(4, 4))
print("INPUT ARRAY : \n", array)
print("\nIndices of min element : ", np.argmin(array, axis = 1))
Output:
Indices of min element : [3 2 3 3]
Using argmin with Condition
An argmin() function can also be applied for a presented array where the items satisfy a particular condition and, the Masked array should be used in Numpy for this purpose. Where the array items have invalid entries those arrays are known as Masked arrays.
“When” the items don't satisfy the presented condition they will not be considered, and we have to mask them. For the rest of the items, the minimum value indices will be returned.
Example:
import numpy as np
array = np.array([10,7,3,11,2,8,12,9])
array = np.ma.MaskedArray(array, array<9)
print(array)
Output:
[10 -- -- 11 -- -- 12 9]
Using argmin with Matrix
The argmin function can also be used with matrix to find the index of minimum elements.
Syntax:
numpy.matrix.argmin
Example:
import numpy as np
matrix = np.matrix(np.arange(25).reshape((5,5)))
print(matrix)
print("\nIndices of min element : ", matrix.argmin())
Output:
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]
[20 21 22 23 24]]
Indices of min element : 0
Conclusion
In the above article, we have learned about the Python argmin() function and understood how to use the argmin() function with different methods.