reduce(method, axis=None, keepdims=False, **kwargs)¶
Performs a reduction operation on this array.
- 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.
The result of the reduction operation.
Return type: Raises:
ValueError– If reducing an all-zero axis would produce a nonzero result.
This function internally calls
COO.sum_duplicatesto bring the array into canonical form.
You can use the
COO.reducemethod to apply a reduction operation to any Numpy
>>> x = np.ones((5, 5), dtype=np.int) >>> s = COO.from_numpy(x) >>> s2 = s.reduce(np.add, axis=1) >>> s2.todense() array([5, 5, 5, 5, 5])
You can also use the
keepdimsargument 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.reducesupports. For example,
dtype. Note that
>>> s4 = s.reduce(np.add, axis=1, dtype=np.float16) >>> s4.dtype dtype('float16')
By default, this reduces the array down to one number, reducing along all axes.
>>> s.reduce(np.add) 25