argmax#

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

Return 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], default: None) – axis along which to search. If None, the function must return the index of the maximum value of the flattened array. Default = None.

  • keepdims (bool, default: False) – 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.

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

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

  • out (Optional[Array], default: None) – If provided, the result will be inserted into this array. It should be of the 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.])
>>> z = ivy.zeros((1,3), dtype=ivy.int64)
>>> ivy.argmax(x, out=z)
>>> print(z)
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((3,1), dtype=ivy.int64)
>>> y = ivy.argmax(x, axis=1, keepdims=True, out=z)
>>> print(z)
ivy.array([[0],[2],[2]])

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([0., -1., 2.]), b=ivy.array([3., 4., 5.]))
>>> y = ivy.argmax(x)
>>> print(y)
{
    a: ivy.array(2),
    b: ivy.array(2)
}
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], default: None) – axis along which to search. If None, the function must return the index of the maximum value of the flattened array. Default: None.

  • keepdims (bool, default: False) – 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.

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

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

  • out (Optional[Array], default: None) – If provided, the result will be inserted into this array. It should be of 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
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[Union[int, Container]], default: None) – axis along which to search. If None, the function must return the index of the maximum value of the flattened array. Default: None.

  • keepdims (Union[bool, Container], default: False) – 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.

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

  • out (Optional[Container], default: None) – If provided, the result will be inserted into this array. It should be of 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]])
}