Articles

argmin in numpy and argmax in numpy | index of minimum and max value in numpy array

argmin in numpy and argmax in numpy | index of minimum and max value in numpy array


In this numpy tutorial,  we will discuss how to apply on the numpy array:

  • argmin in numpyindex of minimum value in numpy arrayand
  • argmax in numpyindex of max value in numpy array.
  • argmax in numpy example,
  • numpy argmin example

Lets learn how to get index of minimum value in numpy array and . But before that lets see about numpy array.

numpy stands for numeric python which is used to perform mathematical operations on arrays.

It is a module in which we have to import from the python.

 

Syntax to import:

import numpy

We can also use alias for the module

 

For example,

import numpy as np

We can directly use np to call the numpy module.

 

Array in Numpy

An array is an one dimensional data structure used to store single data type data.

I.E It will only store all integer data or all string type data.or all float type data.

We can create an numpy array by using array() function.

Syntax:

numpy.array(elements)

where, elements are the input data elements.

 

argmin in numpy

argmin in numpy or argmin() is used to get the index of minimum value in numpy array.

 

Syntax:

numpy.argmin(array_data)

Example: numpy argmin example

Lets use argmin in numpy to get the index of minimum value in numpy array.

#importing the numpy module
import numpy 
 
 #create array with 5 float values
array_data=numpy.array([10.7,20.3,30.2,50.0,78.90])

#actual array
print(array_data)

print()

#get the index of the minimum value
print(numpy.argmin(array_data))

Outputargmin in numpy

The minimum element is 10.7 .so it's index location - 0 is returned.

[10.7 20.3 30.2 50.  78.9]

0

We have learnt how to use argmin in numpy to get index of minimum value in numpy array


argmax in numpy

argmax in numpy or argmax() is used to get the index of max value in numpy array.

Syntax:

numpy.argmax(array_data)

Example: argmax in numpy example

Lets use argmax in numpy to get the index of max value in numpy array.

#importing the numpy module
import numpy 
 
 #create array with 5 float values
array_data=numpy.array([10.7,20.3,30.2,50.0,78.90])

#actual array
print(array_data)

print()

#get the index of the maximum value
print(numpy.argmax(array_data))

Outputargmax in numpy

The maximum element is 78.9  .so it's index location - 4 is returned.

[10.7 20.3 30.2 50.  78.9]

4

Thus we have seen argmax in numpy example.


In 2 D array:

numpy.argmin(array_data,axis)

We can apply argmin() and argmax() functions on a 2 D array.

axis=1 specifies column, in which we will get column wise result.

axis=0 specifies row, in which we will get row wise result.

 

Example 1numpy argmin example in 2D Array

In this numpy argmin example, we will create a 2D array and apply the argmin() function across row and column.

#importing the numpy module
import numpy 
 
 #create array with 5 float values
array_data=numpy.array([[710.7,20.3,30.2,50.0,78.90],[90.7,20.3,30.2,540.0,78.90],[10.7,20.3,570.2,50.0,78.90]])

#actual array
print(array_data)

print()

#get the index of the min value from each columns
print(numpy.argmin(array_data,1))

print()

#get the index of the min value from each rows
print(numpy.argmin(array_data,0))

Output: numpy argmin example

[[710.7  20.3  30.2  50.   78.9]
 [ 90.7  20.3  30.2 540.   78.9]
 [ 10.7  20.3 570.2  50.   78.9]]

[1 1 0]

[2 0 0 0 0]

Example 2argmax in numpy example

In this argmax in numpy example, we will create a 2D array and apply the argmax() function  across row and column.

#importing the numpy module
import numpy 
 
 #create array with 5 float values
array_data=numpy.array([[710.7,20.3,30.2,50.0,78.90],[90.7,20.3,30.2,540.0,78.90],[10.7,20.3,570.2,50.0,78.90]])

#actual array
print(array_data)

print()

#get the index of the max value from each columns
print(numpy.argmax(array_data,1))

print()

#get the index of the max value from each rows
print(numpy.argmax(array_data,0))

Output: argmax in numpy example

[[710.7  20.3  30.2  50.   78.9]
 [ 90.7  20.3  30.2 540.   78.9]
 [ 10.7  20.3 570.2  50.   78.9]]

[0 3 2]

[0 0 2 1 0]

This wraps up our session on argmin in numpy to get index of minimum value in numpy array and argmax in numpy to get index of max value in numpy array.


Numpy

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University. Published 1200+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :    Published Date : Sep 17,2022  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!