×

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 MongoDB Tutorial

An Introduction to MongoDB MongoDB is a document-oriented database application. It is an Open-source and platform-independent program. MongoDB is similar to few NoSQL databases that store the data in the documents...

17 minutes read.

Difference between Sort and Sorted in Python

If you new to Python, it must be confusing the distinction between the Sort and Sorted functions. However, it is important to understand the differences in order to use them...

5 minutes read.

Permutations in Python

Recursion Basic idea: for numbers of length N. N-1 items are chosen at random between 0 and then generate permutations using the remaining N-1 elements in a recursive fashion. Once you've done...

4 minutes read.

Is Python Object Oriented Programming language

In this tutorial, we will see whether python also belongs to an object-oriented programming language like several others. We will also see the advantages of using OOPs. Further, we will...

4 minutes read.

tell() function in Python

Introduction The tell() function in Python is used to return the current position of the file read/write pointer within the file. An integer value is returned by this method, and it...

2 minutes read.

Exponentiation in Python

What is an Exponent in Python? Exponent is a fundamental mathematical concept that is used in many different areas, such as engineering, physics, and finance. In mathematics, an exponent is a...

4 minutes read.

Python Program for Linear Search

Introduction We employ specific algorithms to efficiently carry out our responsibilities, such as searching for an element in a given data structure. These algorithms fall under the category of searching algorithms....

4 minutes read.

How to implement classifiers in Python

What is classification? Classification is a type of supervised machine learning problem with a categorical target (response) variable. Given the known label in the training data, the classifier approximates a mapping...

4 minutes read.

Add in Dictionary Python

Dictionary in python: Dictionaries are a useful data configuration for storing information in Python because they can mimic real-world data arrangements where a particular value exists for a particular key. The...

4 minutes read.

Adding item to a python dictionary

The dictionary is one of python’s built-in data structures where it stores key-value pairs. A dictionary is a collection of ordered values that can be changeable. We can add, modify,...

3 minutes read.

Python Bitwise Operators

When used with variables and objects in an expression, operators are tokens that carry out calculations. The variables and items to which the computation is applied are known as operands....

5 minutes read.

How to Convert A List into String In Python?

How to Convert A List into String In Python? List is a data structure in Python that can hold values of different data types. The values are enclosed in square brackets...

3 minutes read.

Drop() Function in Python

Drop() Function: The python programming language consists of various libraries; some of them are pandas and matplotlib. Data scientists mainly use the pandas library to analyze the data more easily and...

3 minutes read.

Python JSON Schema

Python-jsonschema JSON: JSON stands for JavaScript Object Notation. It is a text-based format that represents structured data and can be used to interchange data among various applications. This is self-defining language...

5 minutes read.

Read Text files in Python

In Python, there are many ways to read text files. Before going into the detailed structure of reading a text file, let us understand how reading text files takes place...

4 minutes read.

Python Data Visualization

In this tutorial, we will understand what data visualization means in python. Further, we will see different methods of visualizing data in python. Data visualization In a non-technical language, it is a...

4 minutes read.

Is Python Case Sensitive

Case sensitivity is the mode of dealing with the written alphabet. The cases of the alphabet are examined and based on these words are being treated. The uppercase and lowercase...

3 minutes read.

Python hash() function

Python hash() function The hash() function in Python returns the hash value of the object (if it has one).  Syntax hash(object) Parameter obj : This parameter represents the object which we need to convert into hash. Return This...

1 minute read.

Cx_Oracle Python with Example

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

6 minutes read.

How to develop a game in python

Fun always makes a task interesting and easy to execute. Learning in the same theoretical way at some point reaches the boring spot. In this article, using some Python knowledge,...

13 minutes read.