argmax

ivy.argmax(x, /, *, axis=None, keepdims=False, dtype=None, select_last_index=False, out=None)[source]

Returns the indices of the maximum values along a specified axis. When the maximum value occurs multiple times, only the indices corresponding to the first occurrence are returned.

Parameters
  • x (Union[Array, NativeArray]) – input array. Should have a numeric data type.

  • axis (Optional[int]) – axis along which to search. If None, the function must return the index of the (default: None) maximum value of the flattened array. Deafult: None.

  • keepdims (bool) – If this is set to True, the axes which are reduced are left in the result as (default: False) dimensions with size one. With this option, the result will broadcast correctly against the array.

  • dtype (Optional[Union[Dtype, NativeDtype]]) – Optional data type of the output array. (default: None)

  • select_last_index (bool) – If this is set to True, the index corresponding to the (default: False) last occurrence of the maximum value will be returned

  • out (Optional[Array]) – If provided, the result will be inserted into this array. It should be of the (default: None) appropriate shape and dtype.

Return type

Array

Returns

ret – if axis is None, a zero-dimensional array containing the index of the first occurrence of the maximum value; otherwise, a non-zero-dimensional array containing the indices of the maximum values. The returned array must have be the default array index data type.

This function conforms to the Array API Standard. This docstring is an extension of the docstring in the standard.

Both the description and the type hints above assumes an array input for simplicity, but this function is nestable, and therefore also accepts ivy.Container instances in place of any of the arguments.

Examples

With ivy.Array input:

>>> x = ivy.array([-0., 1., -1.])
>>> y = ivy.argmax(x)
>>> print(y)
ivy.array([1])
>>> x = ivy.array([-0., 1., -1.])
>>> ivy.argmax(x, out=x)
>>> print(x)
ivy.array([1])
>>> x = ivy.array([[1., -0., -1.], [-2., 3., 2.]])
>>> y = ivy.argmax(x, axis=1)
>>> print(y)
ivy.array([0, 1])
>>> x = ivy.array([[4., 0., -1.], [2., -3., 6]])
>>> y = ivy.argmax(x, axis=1, keepdims=True)
>>> print(y)
ivy.array([[0], [2]])
>>> x = ivy.array([[4., 0., -1.], [2., -3., 6]])
>>> y = ivy.argmax(x, axis=1, dtype=ivy.int64)
>>> print(y, y.dtype)
ivy.array([0, 2]) int64
>>> x = ivy.array([[4., 0., -1.],[2., -3., 6], [2., -3., 6]])
>>> z = ivy.zeros((1,3), dtype=ivy.int64)
>>> y = ivy.argmax(x, axis=1, keepdims=True, out=z)
>>> print(z)
ivy.array([[0],[2],[2]])
ivy.Array.argmax(self, /, *, axis=None, keepdims=False, dtype=None, select_last_index=False, out=None)[source]

ivy.Array instance method variant of ivy.argmax. This method simply wraps the function, and so the docstring for ivy.argmax also applies to this method with minimal changes.

Parameters
  • self (Array) – input array. Should have a numeric data type.

  • axis (Optional[int]) – axis along which to search. If None, the function must return the index of (default: None) the maximum value of the flattened array. Deafult: None.

  • keepdims (bool) – If this is set to True, the axes which are reduced are left in the result as (default: False) dimensions with size one. With this option, the result will broadcast correctly against the array.

  • dtype (Optional[Union[Dtype, NativeDtype]]) – Optional data type of the output array. (default: None)

  • out (Optional[Array]) – If provided, the result will be inserted into this array. It should be of (default: None) the appropriate shape and dtype.

Return type

Union[Array, int]

Returns

ret – if axis is None, a zero-dimensional array containing the index of the first occurrence of the maximum value; otherwise, a non-zero-dimensional array containing the indices of the maximum values. The returned array must have the default array index data type.

Examples

Using ivy.Array instance method:

>>> x = ivy.array([0., 1., 2.])
>>> y = x.argmax()
>>> print(y)
ivy.array(2)
>>> x = ivy.array([[1., -0., -1.], [-2., 3., 2.]])
>>> y = x.argmax(axis=1)
>>> print(y)
ivy.array([0, 1])
>>> x = ivy.array([[4., 0., -1.], [2., -3., 6]])
>>> y = x.argmax(axis=1, keepdims=True)
>>> print(y)
ivy.array([[0], [2]])
>>> x = ivy.array([[4., 0., -1.], [2., -3., 6]])
>>> y = x.argmax(axis=1, dtype=ivy.int64)
>>> print(y, y.dtype)
ivy.array([0, 2]) int64
ivy.Container.static_argmax(x, /, *, axis=None, keepdims=False, dtype=None, select_last_index=False, out=None)[source]

ivy.Container static method variant of ivy.argmax. This method simply wraps the function, and so the docstring for ivy.argmax also applies to this method with minimal changes.

Parameters
  • x (Union[Container, Array, NativeArray]) – input array or container. Should have a numeric data type.

  • axis (Optional[int]) – axis along which to search. If None, the function must return the index of (default: None) the maximum value of the flattened array. Deafult: None.

  • keepdims (bool) – If this is set to True, the axes which are reduced are left in the result as (default: False) dimensions with size one. With this option, the result will broadcast correctly against the array.

  • dtype (Optional[Union[Dtype, NativeDtype]]) – Optional data type of the output array. (default: None)

  • out (Optional[Container]) – If provided, the result will be inserted into this array. It should be of (default: None) the appropriate shape and dtype.

Return type

Container

Returns

ret – a container containing the indices of the maximum values across the specified axis.

Examples

>>> x = ivy.Container(a=ivy.array([[4., 0., -1.], [2., -3., 6]]),    ...                   b=ivy.array([[1., 2., 3.], [1., 1., 1.]])
>>> y = ivy.Container.static_argmax(x, axis=1, keepdims=True)
>>> print(y)
{
    a: ivy.array([[0],
                  [2]]),
    b: ivy.array([[2],
                  [0]])
}
ivy.Container.argmax(self, /, *, axis=None, keepdims=False, dtype=None, select_last_index=False, out=None)[source]

ivy.Container instance method variant of ivy.argmax. This method simply wraps the function, and so the docstring for ivy.argmax also applies to this method with minimal changes.

Parameters
  • self (Container) – input array or container. Should have a numeric data type.

  • axis (Optional[int]) – axis along which to search. If None, the function must return the index of (default: None) the maximum value of the flattened array. Deafult: None.

  • keepdims (bool) – If this is set to True, the axes which are reduced are left in the result as (default: False) dimensions with size one. With this option, the result will broadcast correctly against the array.

  • dtype (Optional[Union[Dtype, NativeDtype]]) – Optional output dtype of the container. (default: None)

  • out (Optional[Container]) – If provided, the result will be inserted into this array. It should be of (default: None) the appropriate shape and dtype.

Return type

Container

Returns

ret – a container containing the indices of the maximum values across the specified axis.

Examples

>>> a = ivy.array([[4., 0., -1.], [2., -3., 6]])
>>> b = ivy.array([[1., 2., 3.], [1., 1., 1.]])
>>> x = ivy.Container(a=a, b=b)
>>> y = x.argmax(axis=1, keepdims=True)
>>> print(y)
{
    a: ivy.array([[0],
                  [2]]),
    b: ivy.array([[2],
                  [0]])
}

Supported Frameworks: