numpy.argmax () in Python

NumPy | Python Methods and Functions

numpy.argmax (array, axis = None, out = None): returns the indices of the max element of the array on a specific axis. 
Parameters:

  array:  Input array to work on  axis:  [int, optional] Along a specified axis like 0 or 1  out:  [array optional] Provides a feature to insert output to the  out  array and it should be of appropriate shape and dtype 

Return:

 Array of indices into the array with same shape as array.shape with the dimension along axis removed. 

Code 1 :

# Python program illustrating
# argmax () work

 

import numpy as geek 

 
# Working with 2D array

a rray = geek.arange ( 12 ). reshape ( 3 , 4 )

print ( "INPUT ARRAY:" , array)

 
# The axis is not specified, so it works on the whole array

print ( "Max element:" , geek.argmax (array))

 
# returns the max element indices
# by indicators

print ( "Indices of Max element: "< / code> , geek.argmax (array, axis = 0 ))

print ( "Indices of Max element:" , geek.argmax (array, axis = 1 ))

Output:

 INPUT ARRAY: [[0 1 2 3] [4 5 6 7] [8 9 10 11]] Max element : 11 Indices of Max element: [2 2 2 2] Indices of Max element: [3 3 3] 

Code 2:

# Python program illustrating
# argmax () work

  

import  numpy as geek 

 
# Working with 2D array

array =   geek.random.randint ( 16 , size = ( 4 , 4 ) )

print ( "INPUT ARRAY: " , array)

  
# The axis is not specified, so it works for the entire array

print ( "Max element:" , geek.argmax (array))

 
# returns the indices of the max element
# by metrics

  
"" "

  [[ 0 3 8 13]

[12 11 2 11]

[5 13 8 3]

  [12 15 3 4]]

^ ^ ^ ^

  12 15 8 13 - element

1 3 0 0 - indicators

"" "

print ( " Indices of Max element: " , geek. argmax (array, axis = 0 ))

 

 
"" "

  ITEM INDEX

- & gt; [[0 3 8 13] 13 3

- & gt; [12 11 2 11] 12 0

- & gt; [5 13 8 3] 13 1

- & gt; [12 15 3 4]] 15 1

 
"" "

print ( "Indices of Max element:" , geek.argmax (array, axis = 1 ))

Output:

 INPUT ARRAY: [[0 3 8 13] [12 11 2 11] [5 13 8 3] [12 15 3 4]] Max element: 15 Indices of Max element: [1 3 0 0] Indices of Max element: [3 0 1 1] 

Code 3:

# Python program illustrating
# argmax () work

 

import numpy as geek 

 
# Working with 2D array

array =   geek.arange ( 10 ). Reshape ( 2 , 5 )

print ( "array:" , array)

 

array [ 0 ] [ 1 ] = 6

print ( " array : " , array)

   # Returns the max element

print ( "array:" , geek.argmax (array))

 
# The first occurrence of the max element given

print ( "MAX ELEMENT INDICES:" , geek.argmax (array, axis = 0 ))

Output:

 array: [[0 1 2 3 4] [5 6 7 8 9]] array: [[0 6 2 3 4] [5 6 7 8 9]] array: 9 MAX ELEMENT INDICES: [1 0 1 1 1] 

Links:
https:// docs. scipy.org/doc/numpy-dev/reference/generated/numpy.argm ax.html # numpy.argmax

Notes:
These codes will not work for online IDs. Please run them on your systems to see how they work

This article is provided by Mohit Gupta_OMG



Get Solution for free from DataCamp guru