numpy.argmax() in Python

The numpy.argmax() function returns the indices of the maximum values along an axis. In case of multiple occurrences of the maximum values, the indices corresponding to the first occurrence will be returned.

Syntax

numpy.argmax(aaxis=Noneout=None)

Parameters

  • array: Input array
  • axis [int, optional]By default, the index is into the flattened array, otherwise along the specified axis.
  • out [array optional]: If provided, the result will be inserted into this array. It should be of the appropriate shape and dtype.

Return Value

An array of indices into the array. It will have the same shape as the array.shape with the dimension along the axis removed.

Finding the maximum element from a matrix with Python numpy.argmax()

import numpy as np    

a = np.matrix([[1,2,3,33],[4,5,6,66],[7,8,9,99]])

print(np.argmax(a))  # 11, which is the position of 99
print(np.argmax(a[:,:]))  # 11, which is the position of 99
print(np.argmax(a[:1]))  # 3, which is the position of 33
print(np.argmax(a[:,2]))  # 2, which is the position of 9
print(np.argmax(a[1:,2]))  # 1, which is the position of 9

Output

11
11
3
2
1

The argmax() returns the position or index of the largest value in an array. The array can be of a single or multidimensional,

Using np.unravel_index on argmax output

We can use the np.unravel_index function for getting an index corresponding to a 2D array from the numpy.argmax output.

import numpy as np
a = np.arange(6).reshape(2,3) + 10
print(a)

index = np.unravel_index(np.argmax(a), a.shape)
print(index)
print(a[index])

Output

[[10 11 12]
 [13 14 15]]
(1, 2)
15

Finding Maximum Elements along columns using Python numpy.argmax()

The below code returns the index value of the maximum elements along each column.

import numpy as np
a = np.arange(12).reshape(4,3) + 10
print(a)

print("Max elements", np.argmax(a, axis=0))

Output

[[10 11 12]
 [13 14 15]
 [16 17 18]
 [19 20 21]]
Max elements [3 3 3]
2 comments
  1. > In simple terms, if you don’t specify the axis to Python’s numpy.argmax() it will return the count of an array.
    Huh, no? The result of np.argmax(np.array([1, 3, 2])) is 1, lol, which is most definitely not the “count” (I assume you meant length?) of the array

    1. Hi Matt,

      The argmax() function returns the index of the maximum value in a given array. Since you have passed an array [1,3,2] the maximum value is 3 which is at an array index of 1 or at position 1. I have stated different examples below

      For 1- dimensional

      a = [[1,2,3,4,5]]
      np.argmax(a)
      >>4

      2 dimensional

      a = [[1,2,3],[4,5,6]]
      np.argmax(a)
      >>5

Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
Python List Pop()

Python List pop()

Table of Contents Hide Syntax of List pop() pop() ParametersReturn Value from List pop()Example 1: Pop item at the given index from the listExample 2: pop() without an index, and for…
View Post
Python String Find()

Python String find()

Table of Contents Hide find() Syntaxfind() Parametersfind() Return ValueDifference between find() method and index() methodExample 1: find() without any argumentsExample 2: find() with start and end Arguments The Python String…
View Post
Python - List Files In A Directory

Python – List Files in a Directory

Table of Contents Hide Method 1: Using os.listdir() methodMethod 2: Using os.walk() methodMethod 3: Using os.scan() methodMethod 4: Using glob module There are several modules available in Python to list…
View Post
Python Read Text File

Python Read Text file

Table of Contents Hide Steps to Read Text File in Python Python open() function Methods for Reading file contentsPython close() functionExamples for Reading a Text file in Python Example 1 – Read…
View Post