Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Numpy exercise
  • pandas
  • Matplotlib
  • Data visulisation
  • EDA
  • Machin Learning
  • Deep Learning
  • NLP
  • Data science
  • ML Tutorial
  • Computer Vision
  • ML project
Open In App
Next Article:
numpy.amin() in Python
Next article icon

numpy.argmax() in Python

Last Updated : 08 Mar, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The numpy.argmax() function returns indices of the max element of the array in a particular axis. 

Syntax : 

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

Parameters : 

array : Input array to work on   axis  : [int, optional]Along a specified axis like 0 or 1  out   : [array optional]Provides a feature to insert output to the out            array and it should be of appropriate shape and dtype

Return :  

Array of indices into the array with same shape as array.shape   with the dimension along axis removed.

Code 1 :  

Python




# Python Program illustrating
# working of argmax()
  
import numpy as geek 
  
# Working on 2D array
array = geek.arange(12).reshape(3, 4)
print("INPUT ARRAY : \n", array)
  
# No axis mentioned, so works on entire array
print("\nMax element : ", geek.argmax(array))
  
# returning Indices of the max element
# as per the indices
print("\nIndices of Max element : ", geek.argmax(array, axis=0))
print("\nIndices of Max element : ", geek.argmax(array, axis=1))
 
 

Output : 

INPUT ARRAY :    [[ 0  1  2  3]   [ 4  5  6  7]   [ 8  9 10 11]]    Max element :  11    Indices of Max element :  [2 2 2 2]    Indices of Max element :  [3 3 3]

Code 2 :  

Python




# Python Program illustrating
# working of argmax()
  
import numpy as geek 
  
# Working on 2D array
array =  geek.random.randint(16, size=(4, 4))
print("INPUT ARRAY : \n", array)
  
# No axis mentioned, so works on entire array
print("\nMax element : ", geek.argmax(array))
  
# returning Indices of the max element
# as per the indices
  
'''   
   [[ 0  3  8 13]
    [12 11  2 11]
    [ 5 13  8  3]
    [12 15  3  4]]
      ^  ^  ^  ^
     12 15  8  13  - element
     1  3   0  0   - indices
'''
print("\nIndices of Max element : ", geek.argmax(array, axis = 0))
  
  
'''   
                            ELEMENT   INDEX
   ->[[ 0  3  8 13]           13        3
    ->[12 11  2 11]           12        0
    ->[ 5 13  8  3]           13        1
    ->[12 15  3  4]]          15        1
        
'''
print("\nIndices of Max element : ", geek.argmax(array, axis = 1))
 
 

Output : 

INPUT ARRAY :    [[ 0  3  8 13]    [12 11  2 11]    [ 5 13  8  3]    [12 15  3  4]]    Max element :  15    Indices of Max element :  [1 3 0 0]    Indices of Max element :  [3 0 1 1]

Code 3 : 

Python




# Python Program illustrating
# working of argmax()
  
import numpy as geek 
  
# Working on 2D array
array =  geek.arange(10).reshape(2, 5)
print("array : \n", array)
  
array[0][1] = 6
print("\narray : \n", array)
       
# Returns max element
print("\narray : ", geek.argmax(array))
  
# First occurrence of an max element is given
print("\nMAX ELEMENT INDICES : ", geek.argmax(array, axis = 0))
 
 

Output : 

array :    [[0 1 2 3 4]   [5 6 7 8 9]]    array :    [[0 6 2 3 4]   [5 6 7 8 9]]    array :  9    MAX ELEMENT INDICES :  [1 0 1 1 1]

Note : 
These codes won’t run on online IDE’s. Please run them on your systems to explore the working.

 



Next Article
numpy.amin() in Python
https://media.geeksforgeeks.org/auth/avatar.png
GeeksforGeeks
Improve
Article Tags :
  • Python
  • Python numpy-Sorting Searching
  • Python-numpy
Practice Tags :
  • python

Similar Reads

  • numpy.amax() in Python
    The numpy.amax() method returns the maximum of an array or maximum along the axis(if mentioned). Syntax: numpy.amax(arr, axis = None, out = None, keepdims = <class numpy._globals._NoValue>) Parameters - arr : [array_like] input dataaxis : [int or tuples of int] axis along which we want the max
    2 min read
  • numpy.argmin() in Python
    The numpy.argmin() method returns indices of the min element of the array in a particular axis. Syntax : numpy.argmin(array, axis = None, out = None) Parameters : array : Input array to work on axis : [int, optional]Along a specified axis like 0 or 1 out : [array optional]Provides a feature to inser
    2 min read
  • numpy.fmax() in Python
    numpy.fmax() function is used to compute element-wise maximum of array elements. This function compare two arrays and returns a new array containing the element-wise maxima. If one of the elements being compared is a NaN, then the non-nan element is returned. If both elements are NaNs then the first
    2 min read
  • numpy.amin() in Python
    The numpy.amin() function returns minimum of an array or minimum along axis(if mentioned). Syntax : numpy.amin(arr, axis = None, out = None, keepdims = <class numpy._globals._NoValue>) Parameters : arr : [array_like]input dataaxis : [int or tuples of int]axis along which we want the min value.
    2 min read
  • numpy.arange() in Python
    numpy.arange() function creates an array of evenly spaced values within a given interval. It is similar to Python's built-in range() function but returns a NumPy array instead of a list. Let's understand with a simple example: [GFGTABS] Python import numpy as np #create an array arr= np.arange(5 , 1
    2 min read
  • numpy.argsort() in Python
    numpy.argsort() is a function in NumPy that returns the indices that would sort an array. In other words, it gives you the indices that you would use to reorder the elements in an array to be in sorted order. Example: [GFGTABS] Python import numpy as geek a = geek.array([2, 0, 1, 5, 4, 1, 9]) print(
    3 min read
  • numpy.argwhere() in Python
    numpy.argwhere() function is used to find the indices of array elements that are non-zero, grouped by element. Syntax : numpy.argwhere(arr) Parameters : arr : [array_like] Input array. Return : [ndarray] Indices of elements that are non-zero. Indices are grouped by element. Code #1 : # Python progra
    1 min read
  • numpy.nanargmax() in Python
    The numpy.nanargmax() function returns indices of the max element of the array in a particular axis ignoring NaNs. The results cannot be trusted if a slice contains only NaNs and Infs. Syntax: numpy.nanargmax(array, axis = None) Parameters : array : Input array to work on axis : [int, optional]Along
    2 min read
  • numpy.nanargmin() in Python
    The numpy.nanargmin() function returns indices of the min element of the array in a particular axis ignoring NaNs. The results cannot be trusted if a slice contains only NaNs and Infs. Syntax: numpy.nanargmin(array, axis = None) Parameters : array : Input array to work on axis : [int, optional]Along
    2 min read
  • numpy.fmin() in Python
    numpy.fmin() function is used to compute element-wise minimum of array elements. This function compare two arrays and returns a new array containing the element-wise minima. If one of the elements being compared is a NaN, then the non-nan element is returned. If both elements are NaNs then the first
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences