COO.sum¶
-
COO.
sum
(axis=None, keepdims=False, dtype=None, out=None)[source]¶ Performs a sum operation along the given axes. Uses all axes by default.
- Parameters
axis (Union[int, Iterable[int]], optional) – The axes along which to sum. Uses all axes by default.
keepdims (bool, optional) – Whether or not to keep the dimensions of the original array.
dtype (numpy.dtype) – The data type of the output array.
- Returns
The reduced output sparse array.
- Return type
See also
numpy.sum
Equivalent numpy function.
scipy.sparse.coo_matrix.sum()
Equivalent Scipy function.
nansum
Function with
NaN
skipping.
Notes
This function internally calls
COO.sum_duplicates
to bring the array into canonical form.
Examples
You can use
COO.sum
to sum an array across any dimension.>>> x = np.ones((5, 5), dtype=np.int) >>> s = COO.from_numpy(x) >>> s2 = s.sum(axis=1) >>> s2.todense() array([5, 5, 5, 5, 5])
You can also use the
keepdims
argument to keep the dimensions after the sum.>>> s3 = s.sum(axis=1, keepdims=True) >>> s3.shape (5, 1)
You can pass in an output datatype, if needed.
>>> s4 = s.sum(axis=1, dtype=np.float16) >>> s4.dtype dtype('float16')
By default, this reduces the array down to one number, summing along all axes.
>>> s.sum() 25