Tutorial – Numpy Argmax, Numpy Argmin, Numpy Max, Numpy Min in Python

Numpy Argmax, Numpy Argmin, Numpy Max, Numpy Min

While working on machine learning or data science projects, you might be required to find the maximum or minimum value or indices in the numpy array. In this post, we will see built-in numpy functions – numpy argmax, numpy argmin, numpy max and numpy min that can help us for this purpose.

In [1]:
import numpy as np

Numpy Argmax : numpy.argmax()

Numpy argmax returns the indices of the maximum value along the axis of a numpy array.

Syntax

numpy.argmax(a, axis=None, out=None)

a – It is an input array.

axis (optional) – It is the index along which the indices of maximum values have to be determined. The input is of type int. By default, the index is into the flattened array, else along the specified axis.

out (optional) – It specifies the array into which the output should be inserted. The size of this array should have the appropriate shape and dtype.

Examples:

Let us first create a sample array first –

In [2]:
arr = np.array([[7,14,57,22] ,[22,25,5,26]])
print(arr)
Out[2]:
[[ 7 14 57 22]
[22 25 5 26]]

Let us now find the maximum value from the entire array. The maximum value is 57 and its position is 2.

In [3]:
np.argmax(arr)
Out[3]:
2

Let us find the indices of maximum value along each of the 4 columns.

In [4]:
np.argmax(arr,axis = 0)
Out[4]:
array([1, 1, 0, 1], dtype=int64)

Let us find the indices of maximum value along each of the 2 rows.

In [5]:
np.argmax(arr,axis = 1)
Out[5]:
array([2, 3], dtype=int64)

Numpy Argmin : numpy.argmin()

Numpy argmin returns the indices of the minimum value along the axis of a numpy array.

Syntax

numpy.argmin(a, axis=None, out=None)

a – It is an input array.

axis (optional) – It is the index along which the indices of minimum values have to be determined. The input is of type int. By default, the index is into the flattened array, else along the specified axis.

out (optional) – It specifies the array into which the output should be inserted. The size of this array should have appropriate shape and dtype.

Examples:

Let us first create a sample array first –

In [6]:
arr = np.array([[7,14,57,22] ,[22,25,5,26]])
print(arr)
Out[6]:
[[ 7 14 57 22]
[22 25 5 26]]

Let us now find the maximum value from the entire array. The minimum value is 5 and its position is 6.

In [7]:
np.argmin(arr)
Out[7]:
6

Let us find the indices of minimum value along each of the 4 columns.

In [8]:
np.argmin(arr,axis = 0)
Out[8]:
array([0, 0, 1, 0], dtype=int64)

Let us find the indices of minimum value along each of the 2 rows.

In [9]:
np.argmin(arr,axis = 1)
Out[9]:
array([0, 2], dtype=int64)

Numpy Max : numpy.max()

Numpy max returns the maximum value along the axis of a numpy array.

Syntax

numpy.max(a, axis=None, out=None, keepdims, initial, where)

a – It is an input array.

axis (optional) – It is the index along which the maximum values have to be determined. The input is of type int. By default, the index is into the flattened array, else along the specified axis. If this is a tuple of ints, then the maximum is selected over multiple axes, instead of a single axis or all the axes as before.

out (optional) – It specifies the array into which the output should be inserted. The size of this array should have appropriate shape and dtype.

keepdims (optional) – If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array. The input type is of bool.

initial (optional) – This specifies the minimum value of an output element. Must be present to allow computation on the empty slice. The input is scalar.

where (optional) – This signifies the elements to compare for the maximum. The input is array_like of bool.

Examples:

Let us first create a sample array first –

In [10]:
arr = np.array([[7,14,57,22] ,[22,25,5,26]])
print(arr)
Out[10]:
[[ 7 14 57 22]
[22 25 5 26]]

Let us now find the maximum value from the entire array. The maximum value is 57.

In [11]:
np.max(arr)
Out[11]:
57

Let us find the maximum value along each of the 4 columns.

In [12]:
np.max(arr, axis=0)
Out[12]:
array([22, 25, 57, 26])

Let us find the maximum value along each of the 2 rows.

In [13]:
np.max(arr, axis=1)
Out[13]:
array([57, 26])

Numpy Min : numpy.min()

Numpy min returns the minimum value along the axis of a numpy array.

Syntax

numpy.min(a, axis=None, out=None, keepdims, initial, where)

a – It is an input array.

axis (optional) – It is the index along which the minimum value has to be determined. The input is of type int. By default, the index is into the flattened array, else along the specified axis. If this is a tuple of ints, then the maximum is selected over multiple axes, instead of a single axis or all the axes as before.

out (optional) – It specifies the array into which the output should be inserted. The size of this array should have the appropriate shape and dtype.

keepdims (optional) – If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array. The input type is of bool.

initial (optional) – This specifies the minimum value of an output element. Must be present to allow computation on the empty slice. The input is scalar.

where (optional) – This signifies the elements to compare for the maximum. The input is array_like of bool.

Examples:

Let us first create a sample array first –

In [14]:
arr = np.array([[7,14,57,22] ,[22,25,5,26]])
print(arr)
Out[14]:
[[ 7 14 57 22]
[22 25 5 26]]

Let us now find the maximum value from the entire array. The maximum value is 5.

In [15]:
np.min(arr)
Out[15]:
5

Let us find the minimum value along each of the 4 columns.

In [16]:
np.min(arr, axis=0)
Out[16]:
array([ 7, 14, 5, 22])

Let us find the maximum value along each of the 2 rows.

In [17]:
np.min(arr, axis=1)
Out[17]:
array([7, 5])

Conclusion

I hope it was a good tutorial. People often get confused between these, so do remember that numpy argmax and numpy argmin return the indices of the maximum and minimum value from the array. On the other hand numpy max and numpy min return the maximum and minimum value from the array.

Click Here To Download This Tutorial in Interactive Jupyter Notebook

 

Reference-  https://numpy.org/doc/

Follow Us

Leave a Reply

Your email address will not be published. Required fields are marked *