Now Reading
Hands-On Guide To Use Argmax() Function In Python

Hands-On Guide To Use Argmax() Function In Python

Argmax() Function In Python

There are a variety of inbuilt functions in python that can be used to reduce the manual effort with easy and quick implementation. These inbuilt functions can be used by calling the libraries and corresponding functions. The ‘argmax()’ is one of these inbuilt functions in NumPy. It is not only very easy to implement but has very key importance in programming when the programming handles the numbers and data. Throughout this article, we will discuss the importance of the ‘argmax()’ function and demonstrate its implementation through examples. 

Topics we cover in this article:-

  • The Numpy Library
  • About argmax 
  • argmax() Function Syntax
  • Different ways of using argmax()

The Numpy Library

What is the importance of Numpy in the context of the argmax() function…? Because argmax() is an inbuilt function in the Numpy library.

Follow us on Google News>>

Numpy is an open-source library in Python programming language that supports large mathematical operations and capable of handling huge amounts of data in the form of arrays, multidimensional arrays. Numpy also works well in the manipulation of data like reshaping and matrix multiplications. 

Installing and importing NumPy

Here is the way to install and use the NumPy library.

Looking for a job change? Let us help you.
pip install numpy
import numpy as np

About argmax() function

‘argmax()’ is a mathematical operation that gives the maximum value from the given set of arguments. For example, if we are working on an image classification problem we need to find the object which has the highest confidence score and that confidence score is the probability values that are structured in the array format for each object. For example, in the Imagenet dataset, there are nearly 1000 classes labels of objects. 

So, upon the prediction of the class labels for an object by a machine learning model, it becomes very hard to find manually which class label has the highest probability score. Here we use argmax() function to get the highest probability value of the class labels in the array for an object. In argmax() there are some parameters by default and based on the requirement, we can change these parameters.

Syntax of argmax() function

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

  • a – an input array, in that input array argmax() filters the highest value as output.
  • axis-  it can be set by either 1 or 0, by default, we get the output as a flattened array otherwise we need to set the axis = 1.
  • Out – Before using this parameter we need to create one dummy variable, and it directly stores the output in the given variable. 

User Defined Function for argmax() 

def argmax(array):
 index, value = 0, array[0]
 for i,v in enumerate(array):
   if v > value:
     index, value = i,v
 return index

Now, we will check the above-defined function on a randomly defined array. 

# define array
array = [1, 2, 3, 4, 5]
result = argmax(array)
print (result)

value= array[result]
print ('maximum value %s : index %d' % (value,result))

argmax in python


Implementation of argmax() using numpy

from numpy import argmax
# define vector
vector = [0.4, 0.5, 0.1]

# get argmax
result = argmax(vector)
value = vector[result]

print ('maximum value %s : index %d' % (value,result))


In the next step,  we will take a random 2D array and try to demonstrate the difference in setting the parameter to axis = 1 and axis = 0.

import numpy as np 
array =  np.random.randint(9, size=(3, 3)) 
print("INPUT ARRAY : \n", array)  
max_index = (np.argmax(array ,axis= 1))
print("Indices of Max element when axis=1 : ",(max_index))
print("\nIndices of Max element when axis =0: ", np.argmax(array, axis = 0))


In the above output, if the axis = 1, then it finds out the maximum value in a vertical axis. If the axis = 0, then it finds out the maximum value in the horizontal axis.


In the above demonstration, we could discuss the ‘argmax()’ function and its implementation using a manually defined function and using the NumPy library with proper examples. We can conclude that the ‘argmax()’ function returns the index of the maximum value from the given array. With the help of this, we can find out the maximum value from a given array of any length, very easily and quickly.

What Do You Think?

Join Our Discord Server. Be part of an engaging online community. Join Here.

Subscribe to our Newsletter

Get the latest updates and relevant offers by sharing your email.

Copyright Analytics India Magazine Pvt Ltd

Scroll To Top