What is the nump.argmax() function in NumPy?
Overview
The argmax() function in NumPy is simply used to return the
Syntax
numpy.argmax(a, axis=None, out=None, *, keepdims=<no value>)
Required parameter value
The argmax() function takes a mandatory parameter value, a, representing the input array.
Optional parameter value
The argmax() function takes the following optional parameter values:
axis: By default, the index is into the flattened array, and is otherwise along the specified axis.keepdims: This takes aBooleanvalue (TrueorFalse). If set toTrue, the reduced axis is left as dimensions with size one.
Return value
The argmax() function returns an array of indices into the array.
Code example
import numpy as np# creating a 2D arraymyarray = np.arange(6).reshape(2,3) + 1# implementing the argmax() functionnewarray1 = np.argmax(myarray, axis=None)# at axis 0newarray2 = np.argmax(myarray, axis=0)# at axis 1newarray3 = np.argmax(myarray, axis=1)print(myarray)print(newarray1)print(newarray2)print(newarray3)
Code example
-
Line 1: We import the
numpymodule. -
Line 3: We create a
2Darray,myarray, with a sequence of elements starting from1to6using thereshape()and thearange()functions. -
Line 6: We implement the
argmax()function on the arraymyarray, setting axis value asNone. The result is assigned to a variablenewarray1. -
Line 9: We implement the
argmax()function on the arraymyarray, setting the axis value as0. The result is assigned to a variablenewarray2. -
Line 12: We implement the
argmax()function on the arraymyarray, setting the axis value as1. The result is assigned to a variablenewarray3. -
Lines 14-17: We print all the arrays we created in the code.