Javatpoint Logo
Javatpoint Logo

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:

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:

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:

Output:

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

Example 3:

Output:

(3, 4)
26

Example 4:

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.


Next TopicNumpy.diff()





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA