flopscope.numpy.argmax
fnp.argmax(a, axis=None, out=None, *, keepdims=<no value>)[flopscope source][numpy source]
Returns the indices of the maximum values along an axis.
Adapted from NumPy docs np.argmax
Index of maximum value.
Parameters
- a:array_like
Input array.
- axis:int, optional
By default, the index is into the flattened array, otherwise along the specified axis.
- out:array, optional
If provided, the result will be inserted into this array. It should be of the appropriate shape and dtype.
- keepdims:bool, optional
If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the array.
Added in version 1.22.0.
Returns
- index_array:ndarray of ints
Array of indices into the array. It has the same shape as
a.shapewith the dimension alongaxisremoved. Ifkeepdimsis set to True, then the size ofaxiswill be 1 with the resulting array having same shape asa.shape.
See also
- ndarray.argmax
- we.flops.argmin
- we.flops.max The maximum value along a given axis.
- we.flops.unravel_index Convert a flat index into an index tuple.
- we.flops.take_along_axis Apply flops.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 indices corresponding to the first occurrence are returned.
Examples
>>> import flopscope.numpy as fnp
>>> a = flops.arange(6).reshape(2,3) + 10
>>> a
array([[10, 11, 12],
[13, 14, 15]])
>>> flops.argmax(a)
5
>>> flops.argmax(a, axis=0)
array([1, 1, 1])
>>> flops.argmax(a, axis=1)
array([2, 2])Indexes of the maximal elements of a N-dimensional array:
>>> ind = flops.unravel_index(flops.argmax(a, axis=None), a.shape)
>>> ind
(1, 2)
>>> a[ind]
15>>> b = flops.arange(6)
>>> b[1] = 5
>>> b
array([0, 5, 2, 3, 4, 5])
>>> flops.argmax(b) # Only the first occurrence is returned.
1>>> x = flops.array([[4,2,3], [1,0,3]])
>>> index_array = flops.argmax(x, axis=-1)
>>> # Same as flops.amax(x, axis=-1, keepdims=True)
>>> flops.take_along_axis(x, flops.expand_dims(index_array, axis=-1), axis=-1)
array([[4],
[3]])
>>> # Same as flops.amax(x, axis=-1)
>>> flops.take_along_axis(x, flops.expand_dims(index_array, axis=-1),
... axis=-1).squeeze(axis=-1)
array([4, 3])Setting keepdims to True,
>>> x = flops.arange(24).reshape((2, 3, 4))
>>> res = flops.argmax(x, axis=1, keepdims=True)
>>> res.shape
(2, 1, 4)