One of the important metrics that is ubiquitous in analysing data is the maximum. Knowing this measure is critical in multiple aspects of machine learning. One such aspect would be to foresee the bias in the results that a maximum value could inflict.
But what if one would like to go beyond finding the maximum value such as finding its index in the given dataset or even finding the indices of all the maximum values along an axis? Does Python have a function that could lend a helping hand? Seems that there is!
Enter the argmax( ) function. This function successfully returns the indices of the maximum values along the specified axis. Hailing from the numpy library, one ought to import this library first before putting the function into use. It can be done using the below code.
import numpy as np
This article sets out to explore the argmax( ) function in depth.
Also read: Python String Interpolation
Syntax of the argmax( ) function
Given below is the syntax of the argmax( ) function containing its mandatory and optional constructs required for its proper functioning.
numpy.argmax(a, axis=None, keepdims=<no value>)
- a – input array for which the indices of the maximum values are to be found along an axis
- axis – Set to ‘None’ by default, it is used to specify the axis along which the indices of the maximum values are to be returned
- keepdims – Set to ‘No Value’ by default, it is used to leave the reduced axes as dimensions with size one & broadcast the result correctly against the input array
Use cases for the argmax( ) function
To comprehend the results of this function, it is a prerequisite to understand the logic upon which it works. For any given array, indices are those which indicate the location at which each entity within the array resides.
Normal practice is to start from the left and move towards the right while numbering the indices. This leaves the leftmost value with zero (0) as its index and gets incremented by one as it moves towards the right.
Similarly while moving downwards instead of sideways, the same logic is applied. The topmost value is set with zero (0) as its index and gets incremented by one as it moves downwards. To move sideways from left to right, the axis is to be set to one (1), whereas if one would like to move downwards then the axis value to be set is zero (0).
Using on 1-Dimensional array
Let’s get started with a 1-Dimensional array such as the one given below.
ar1 = np.array([-1, 3, 67, 9])
Now, let’s find the index of its maximum value using the argmax( ) function.
It could be seen from the above result that the index ‘2’ of the maximum value ‘67’ is returned owing to its position from the leftmost value in the array. The index of the leftmost value is zero & ‘67’ being third in position from the leftmost value gets ‘2’ as its index.
Using on N-Dimensional array
The axis option cannot be fully exercised in a 1-Dimensional array since it has only one row. Let’s find the indices of maximum values along both axes for the below N-Dimensional array.
ar2 = np.array([[0, 23, 4, -9], [5, 7, 89, 3], [-4, 10, 9, 78]])
Now, it’s time to find the indices of maximum values sideways (i.e) axis=1.
The index of ‘23’ in the first row is ‘1’ and those of ‘89’ in the second row & ‘78’ in the third row are ‘2’ and ‘3’ respectively. The same has been returned in the result as shown above. One can also find the indices of the maximum values along each column by setting axis=0.
Now that we have reached the end of this article, hope it has elaborated on how to use the argmax( ) function from the numpy library. Here’s another article that details the workings of the linalg.tensorinv( ) function from the numpy library within Python. There are numerous other enjoyable and equally informative articles in Codeforgeek that might be of great help to those who are looking to level up in Python. Carpe diem!