# 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]``````

Get notified of the best deals on our WordPress themes.

##### You May Also Like ## JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Table of Contents Hide JSONDecodeError: Expecting value: line 1 column 1 (char 0)Example JSONDecodeError: Expecting value: line 1 column 1 (char 0)Solution Other Possible SolutionsEnsure HTTP 200 status code and valid… ## Python ValueError: could not convert string to float

Table of Contents Hide ValueError: could not convert string to floatException could not convert string to float Fix ValueError: could not convert string to floatSolution 1: Ensure the string has… ## ImportError: attempted relative import with no known parent package

Table of Contents Hide How does module import work in Python?Absolute vs. Relative importsHow to fix ImportError: attempted relative import with no known parent package?Option 1 – Use absolute importsOption… ## List of Free Python Books

Table of Contents Hide Python SuccinctlyPython Data Science HandbookThink Python 2nd EditionAutomate the Boring Stuff with PythonMaking Games with Python & PygameData Structures and Algorithms in Python Google Python Style GuideA… ## Python FileNotFoundError: [Errno 2] No such file or directory Solution

Table of Contents Hide Python FileNotFoundError: [Errno 2] No such file or directoryExample FileNotFoundErrorMisspelled file nameInvalid file path or directory pathUsing a relative pathSolution to FileNotFoundError: [Errno 2] No such… ## TypeError: list indices must be integers or slices, not tuple

If you are accessing the list elements in Python, you need to access it using its index position. If you specify a tuple or a list as an index, Python…