COO.reduce

COO.reduce(method, axis=(0, ), keepdims=False, **kwargs)[source]

Performs a reduction operation on this array.

Parameters
  • method (numpy.ufunc) – The method to use for performing the reduction.

  • axis (Union[int, Iterable[int]], optional) – The axes along which to perform the reduction. Uses all axes by default.

  • keepdims (bool, optional) – Whether or not to keep the dimensions of the original array.

  • kwargs (dict) – Any extra arguments to pass to the reduction operation.

Returns

The result of the reduction operation.

Return type

COO

Raises

ValueError – If reducing an all-zero axis would produce a nonzero result.

Notes

This function internally calls COO.sum_duplicates to bring the array into canonical form.

See also

numpy.ufunc.reduce()

A similar Numpy method.

COO.nanreduce()

Similar method with NaN skipping functionality.

Examples

You can use the COO.reduce method to apply a reduction operation to any Numpy ufunc.

>>> x = np.ones((5, 5), dtype=np.int)
>>> s = COO.from_numpy(x)
>>> s2 = s.reduce(np.add, axis=1)
>>> s2.todense()  # doctest: +NORMALIZE_WHITESPACE
array([5, 5, 5, 5, 5])

You can also use the keepdims argument to keep the dimensions after the reduction.

>>> s3 = s.reduce(np.add, axis=1, keepdims=True)
>>> s3.shape
(5, 1)

You can also pass in any keyword argument that numpy.ufunc.reduce supports. For example, dtype. Note that out isn’t supported.

>>> s4 = s.reduce(np.add, axis=1, dtype=np.float16)
>>> s4.dtype
dtype('float16')

By default, this reduces the array by only the first axis.

>>> s.reduce(np.add)
<COO: shape=(5,), dtype=int64, nnz=5, fill_value=0>