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

Throughout this article, we will discuss the importance of the ‘argmax()’ function and demonstrate its implementation through examples. 
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.

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. 

Subscribe to our Newsletter

Join our editors every weekday evening as they steer you through the most significant news of the day, introduce you to fresh perspectives, and provide unexpected moments of joy
Your newsletter subscriptions are subject to AIM Privacy Policy and Terms and Conditions.

Installing and importing NumPy

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

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.

Prudhvi varma
AI enthusiast, Currently working with Analytics India Magazine. I have experience of working with Machine learning, Deep learning real-time problems, Neural networks, structuring and machine learning projects. I am a Computer Vision researcher and I am Interested in solving real-time computer vision problems.

Download our Mobile App


AI Hackathons, Coding & Learning

Host Hackathons & Recruit Great Data Talent!

AIM Research

Pioneering advanced AI market research

Request Customised Insights & Surveys for the AI Industry


Strengthen Critical AI Skills with Trusted Corporate AI Training

Our customized corporate training program on Generative AI provides a unique opportunity to empower, retain, and advance your talent.

AIM Leaders Council

World’s Biggest Community Exclusively For Senior Executives In Data Science And Analytics.

3 Ways to Join our Community

Telegram group

Discover special offers, top stories, upcoming events, and more.

Discord Server

Stay Connected with a larger ecosystem of data science and ML Professionals

Subscribe to our Daily newsletter

Get our daily awesome stories & videos in your inbox