# 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(a, axis=None, out=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]``````

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

## How to rename columns in Pandas DataFrame

Table of Contents Hide Method 1: Rename Specific column names in Pandas DataFrameMethod 2: Rename all column names in Pandas DataFrameMethod 3: Replace specific characters in Columns of Pandas DataFrame…

## TypeError: unhashable type: ‘list’

Table of Contents Hide TypeError: unhashable type: ‘list’Example – unhashable type: ‘list’Solution to TypeError: unhashable type: ‘list’.Solution 1 – By Converting list into a tupleSolution 2 – By Adding list…

## [Solved] AttributeError: ‘float’ object has no attribute ‘get’

Table of Contents Hide What is AttributeError: ‘float’ object has no attribute ‘get’?How to fix AttributeError: ‘float’ object has no attribute ‘get’?Solution 1 – Call the get() method on valid…

## How to get current directory in Python?

Table of Contents Hide Getting the Current Working Directory in PythonGet the path of the script file in PythonChanging the Current Working Directory in Python Conclusion In this article, we will…

## Python ord(): A Step-By-Step Guide

Table of Contents Hide ord() Function in Python Example ord() vs chr() ord() Function ExamplesTypeError: ord() expected a character, but string of length 2 found. In Python ord() function accepts a single…