numpy.argmax#
- numpy.argmax(a,axis=None,out=None,*,keepdims=<novalue>)[source]#
Returns the indices of the maximum values along an axis.
- Parameters:
- aarray_like
Input array.
- axisint, optional
By default, the index is into the flattened array, otherwisealong the specified axis.
- outarray, optional
If provided, the result will be inserted into this array. It shouldbe of the appropriate shape and dtype.
- keepdimsbool, optional
If this is set to True, the axes which are reduced are leftin the result as dimensions with size one. With this option,the result will broadcast correctly against the array.
New in version 1.22.0.
- Returns:
- index_arrayndarray of ints
Array of indices into the array. It has the same shape as
a.shapewith the dimension alongaxis removed. Ifkeepdims is set to True,then the size ofaxis will be 1 with the resulting array having sameshape asa.shape.
See also
ndarray.argmax,argminamaxThe maximum value along a given axis.
unravel_indexConvert a flat index into an index tuple.
take_along_axisApply
np.expand_dims(index_array,axis)from argmax to an array as if by calling max.
Notes
In case of multiple occurrences of the maximum values, the indicescorresponding to the first occurrence are returned.
Examples
>>>importnumpyasnp>>>a=np.arange(6).reshape(2,3)+10>>>aarray([[10, 11, 12], [13, 14, 15]])>>>np.argmax(a)5>>>np.argmax(a,axis=0)array([1, 1, 1])>>>np.argmax(a,axis=1)array([2, 2])
Indexes of the maximal elements of a N-dimensional array:
>>>ind=np.unravel_index(np.argmax(a,axis=None),a.shape)>>>ind(1, 2)>>>a[ind]15
>>>b=np.arange(6)>>>b[1]=5>>>barray([0, 5, 2, 3, 4, 5])>>>np.argmax(b)# Only the first occurrence is returned.1
>>>x=np.array([[4,2,3],[1,0,3]])>>>index_array=np.argmax(x,axis=-1)>>># Same as np.amax(x, axis=-1, keepdims=True)>>>np.take_along_axis(x,np.expand_dims(index_array,axis=-1),axis=-1)array([[4], [3]])>>># Same as np.amax(x, axis=-1)>>>np.take_along_axis(x,np.expand_dims(index_array,axis=-1),...axis=-1).squeeze(axis=-1)array([4, 3])
Settingkeepdims toTrue,
>>>x=np.arange(24).reshape((2,3,4))>>>res=np.argmax(x,axis=1,keepdims=True)>>>res.shape(2, 1, 4)