×

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

Add Dictionary to Dictionary in Python

Dictionary in python: Python's execution of a data model, known more commonly as an implicit array, is a dictionary. A dictionary is made up of a group of key-value pairs. Each...

4 minutes read.

Python Dictionary values() method

Python Dictionary values() method The dictionary.values() method in Python returns a view object that displays a list of all the values in the specified dictionary. Syntax dictionary.values() Parameter NA Return This method returns a view object. The...

1 minute read.

Accuracy_score Function in Sklearn

A crucial stage in data science is measuring our model's performance using the appropriate metric. In this article, we will examine two methods for calculating the accuracy of your predictions:...

13 minutes read.

CSV Write in Python

What is meant by CSV? CSV stands for Comma Separated Values. The name itself defines its purpose. CSV arranges the data in the form of tables and stores the organized data in...

3 minutes read.

Return Statement In Python

Python Return Statement The return statement is generally used for the execution ending and returns the value back to the caller. The return statement can return all types of values and...

2 minutes read.

Python Syntax

Python is a strong object-oriented programming language that is simple to learn. Python was created to be a very readable programming language. The syntax of the Python programming language is...

8 minutes read.

Compound Interest GUI Calculator using Tkinter 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...

6 minutes read.

Python md5_file() function

Python md5_file() function The md5_file() function in PHP calculates the md5 hash of a given file. Syntax md5_file ( string $filename [, bool $raw_output ] )  Parameter filename(required)- This parameter signifies the file to be calculated. raw_output(optional)- It takes a boolean value that specifies hex or binary...

1 minute 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.

Python String index() method

Python String index() method The string.index() method in Python finds the lower index of the first occurrence of the specified value or raise a ValueError if the substring is not found. Syntax index(sub[, start[, end]]) Parameter sub:...

2 minutes read.

Python divmod() function

Python divmod() function The divmod() function in Python returns a tuple containing the quotient  and the remainder when parameter ‘a’ (divident) is divided by parameter ‘b’ (divisor). Syntax: divmod(a, b) Parameter a: This parameter represents a number you...

1 minute read.

Reason for Python So Popular

One of the languages that is witnessing outstanding development and acceptance every year in Python. Python has emerged as the programming language with the greatest rate of growth, and Stack...

3 minutes read.

Filter List in Python

Python: Python is one of the most used programming languages, as it is used widely in software and data analysis, web development, etc. It is said to be a user-friendly programming...

3 minutes read.

Method Overloading in Python

Overloading is a capability of a method and operator to behave differently according to the nature of parameters. Overloading is popular because it provides a good number of advantages: Reusability of code.Reduces...

3 minutes read.

Python Set isdisjoint() method

Python Set isdisjoint() method The set.isdisjoint() method in Python returns a boolean value True if two sets are disjoint sets ( i.e. none of the elements are present in both sets), otherwise it returns...

1 minute read.

Screen Rotation app Using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

3 minutes read.

Subprocess Module in Python

What is a Process? Every program will have its respective system state, i.e., the state in which it is running. A program that is in a running state or in a...

7 minutes read.

Python String swapcase() method

Python String swapcase() method The string.swapcase() method in Python returns a copy of the string with uppercase characters converted to lowercase and vice versa. Syntax string.swapcase() Parameter NA Return This method returns a copy of the string...

1 minute read.

Python Escape Characters

In this tutorial, we will learn how to use the Escape Characters in Python. Escape Character: Escape Characters are used for some special meaning in our statements. It is denoted or represented...

3 minutes read.

Python Os sep

Python: 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 a faster way....

3 minutes read.