reduce#

ivy.reduce(operand, init_value, computation, /, *, axes=0, keepdims=False)[source]#

Reduces the input array’s dimensions by applying a function along one or more axes.

Parameters:
  • operand (Union[Array, NativeArray]) – The array to act on.

  • init_value (Union[int, float]) – The value with which to start the reduction.

  • computation (Callable) – The reduction function.

  • axes (Union[int, Sequence[int]], default: 0) – The dimensions along which the reduction is performed.

  • 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.

Return type:

Array

Returns:

ret – The reduced array.

Examples

>>> x = ivy.array([[1, 2, 3], [4, 5, 6]])
>>> ivy.reduce(x, 0, ivy.add, 0)
ivy.array([6, 15])
>>> x = ivy.array([[1, 2, 3], [4, 5, 6]])
>>> ivy.reduce(x, 0, ivy.add, 1)
ivy.array([5, 7, 9])
Array.reduce(self, init_value, computation, /, *, axes=0, keepdims=False)[source]#

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

Parameters:
  • self (Array) – The array to act on.

  • init_value (Union[int, float]) – The value with which to start the reduction.

  • computation (Callable) – The reduction function.

  • axes (Union[int, Sequence[int]], default: 0) – The dimensions along which the reduction is performed.

  • 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.

Return type:

Array

Returns:

ret – The reduced array.

Examples

>>> x = ivy.array([[1, 2, 3], [4, 5, 6]])
>>> x.reduce(0, ivy.add, 0)
ivy.array([6, 15])
Container.reduce(self, init_value, computation, /, *, axes=0, keepdims=False, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False)[source]#

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

Parameters:
  • self (Container) – The array to act on.

  • init_value (Union[int, float, Container]) – The value with which to start the reduction.

  • computation (Union[Callable, Container]) – The reduction function.

  • axes (Union[int, Sequence[int], Container], default: 0) – The dimensions along which the reduction is performed.

  • 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.

  • key_chains (Optional[Union[List[str], Dict[str, str], Container]], default: None) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (Union[bool, Container], default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • prune_unapplied (Union[bool, Container], default: False) – Whether to prune key_chains for which the function was not applied. Default is False.

  • map_sequences (Union[bool, Container], default: False) – Whether to also map method to sequences (lists, tuples). Default is False.

Return type:

Container

Returns:

ret – The reduced array.

Examples

>>> x = ivy.Container(
...     a=ivy.array([[1, 2, 3], [4, 5, 6]]),
...     b=ivy.native_array([[7, 8, 9], [10, 5, 1]]))
>>> y = x.reduce(0, ivy.add)
>>> print(y)
{
    a: ivy.array([5, 7, 9]),
    b: ivy.array([17, 13, 10])
}