numpy.argmax in Python

In many cases, where the size of the array is too large, it takes too much time to find the maximum elements from them. For this purpose, the numpy module of Python provides a function called numpy.argmax(). This function returns indices of the maximum values are returned along with the specified axis.

numpy argmax

Syntax:

snippet
numpy.argmax(a, axis=None, out=None)

Parameters

x: array_like

This parameter defines the source array whose maximum value we want to know.

axis: int(optional)

This parameter defines the axis along which the index is present, and by default, it is into the flattened array.

out: array(optional)

This parameter defines the ndarray in which the result is going to be inserted. This will be of the same type and shape, which is appropriate for storing the result

Returns

This parameter defines a ndarray, which contains the indices of the array. The shape is the same as x.shape with the dimension along the axis removed.

Example 1:

snippet
Import numpy as np
x = np.arange(20).reshape(4,5) + 7
x
y=np.argmax(a)
y

Output:

Output
array([[ 7, 8, 9, 10, 11], [12, 13, 14, 15, 16], [17, 18, 19, 20, 21], [22, 23, 24, 25, 26]]) 19

In the above code

  • We have imported numpy with alias name np.
  • We have created an array 'x' using np.arange() function with the shape of four rows and five columns.
  • We have added 7 in each element of the array also.
  • We have declared the variable 'y' and assigned the returned value of np.argmax() function.
  • We have passed the array 'x' in the function.
  • Lastly, we tried to print the value of 'y'.

In the output, it shows the indices of the maximum element in the array.

Example 2:

snippet
Import numpy as np
x = np.arange(20).reshape(4,5) + 7
y=np.argmax(x, axis=0)
z=np.argmax(x, axis=1)
y
z

Output:

Output
array([3, 3, 3, 3, 3], dtype=int64) array([4, 4, 4, 4], dtype=int64)

Example 3:

snippet
Import numpy as np
x = np.arange(20).reshape(4,5) + 7
indices = np.unravel_index(np.argmax(x, axis=None), x.shape)
indices
x[indices]

Output:

Output
(3, 4) 26

Example 4:

snippet
import numpy as np
a = np.array([[5,2,1], [3,7,9],[0, 4, 6]])
index_arr = np.argmax(a, axis=-1)
index_arr
# Same as np.max(a, axis=-1, keepdims=True)
result = np.take_along_axis(a, np.expand_dims(index_arr, axis=-1), axis=-1)
result1
# Same as np.max(a, axis=-1)
result = np.take_along_axis(a, np.expand_dims(index_arr, axis=-1), axis=-1).squeeze(axis=-1)
result2

Output:

Output
array([[0], [2], [2]]) array([5, 9, 6])

In the above code

  • We have imported numpy with alias name np.
  • We have created a multi-dimensional array 'a' using np.array() function.
  • We have declared the variable 'index_arr' and assigned the returned value of np.argmax() function.
  • We have passed the array 'a' and the axis in the function.
  • We tried to print the value of 'index_arr'.
  • In the end, we tried to fetch the maximum value of the array with the help of two different ways, which are quite similar to np.argmax().

In the output, it shows indices of the maximum elements in the array and the values which are present on that indices.

Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +