×

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.


Related Topics

Python vs Java

There are a lot of programming languages out there, and every day, a new programming language is developing in some parts of the world. Each programming language is a mixture...

5 minutes read.

ER diagram of the Bank Management System in python

What is an ER diagram? The full form of the ER diagram is the Entity Relationship diagram. This diagram is used in database management systems to have a rough idea of...

3 minutes read.

Add a key-value pair to dictionary in Python

In programming, data type defines the type of value that a variable can hold. With help of these, we can perform various mathematical, logical, or relational operations on that particular...

5 minutes read.

Python Set Methods

[et_pb_section][et_pb_row][et_pb_column type="4_4"][et_pb_text] Python Set Methods A set is a collection which is unordered and unindexed. Python has a set of built-in methods that you can use on sets. Methods ...

4 minutes read.

GUI Calendar using PyQt5 in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

3 minutes read.

Python EOL (End Of Line)

Introduction An EOL (End Of Line) is defined as a syntax error that indicates that the Python interpreter reached at the end of the line when it tried to scan a...

3 minutes read.

Difference between python list and tuple

In this tutorial, we will understand the key differences between python lists and tuples in python. Firstly, let us see the similarities between Lists and Tuples. Both are two data types...

4 minutes read.

Python RegEx

Python RegEx (python regular expressions) is a concept of writing and finding expressions in a pool of characters easily. A Regular expression (RegEx) is a set of characters that defines search...

10 minutes read.

Python String startswith() method

Python String startswith() method The string.startswith() method in Python returns a boolean value ‘True’ if the given string starts with the prefix, otherwise it returns False. Syntax startswith(prefix[, start[, end]]) Parameter prefix: This parameter signifies the value to check. start(optional):...

1 minute read.

Python pynmea2

Define Pynmea2 Python Programming Language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in...

4 minutes read.

Difference between Input() and raw_input() functions in Python

There are two input functions in Python that are used for taking input are given below: raw_input()input() What is raw_input() Function? raw_input() function is a built-in function in Python. It is used to...

6 minutes read.

Python Program to check whether a given number is Armstrong or not

Program to check whether a given number is Armstrong or not A number is said to be an Armstrong if the sum of each digit's cube of a given number equals...

1 minute read.

Iterate through the list in Python

One of the six basic data types of the Python computer language is the list. You must be familiar with the methods and functions that deal with lists in order...

7 minutes read.

Python try catch exception

The try-except proclamation can deal with exceptions. Exceptions might happen when you run a program. Exceptions are blunders that occur during the execution of the program. Python won't educate you regarding...

3 minutes read.

Python Set union() method

Python Set union() method The set.union() method in Python returns a set that contains all items from the original set and all items from the specified sets. Syntax set.union(set1, set2...) Parameter set1- This parameter represents the first set...

2 minutes read.

Python List pop() method

Python List pop() method The list.pop() method removes the item at the specified position in the list, and return it. If no index is specified, this method removes and returns the last item in the list. Syntax list.pop([i]) Parameter i:...

1 minute read.

AX Contour in Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

Python next() function

Python next() function The next() function in Python retrieves the next item from the iterator.  Syntax next(iterator[, default]) Parameter iterable: It is a required parameter which represents an iterable object. default: This parameter represents a default value to...

1 minute read.

Palindrome program in Python

Palindrome program in python A number or string is said to be a palindrome if we invert the number or string and the string or number remains the same as the...

3 minutes read.

Python Logical Operators

In python, there are many operators which we use in our program. Operators are used in manipulating a particular value or operand. Logical operators are used mainly to evaluate an...

7 minutes read.