COO.prod¶
-
COO.
prod
(axis=None, keepdims=False, dtype=None, out=None)[source]¶ Performs a product operation along the given axes. Uses all axes by default.
Parameters: - axis (Union[int, Iterable[int]], optional) – The axes along which to multiply. 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.prod
- Equivalent numpy function.
nanprod
- Function with
NaN
skipping.
Notes
- This function internally calls
COO.sum_duplicates
to bring the array into canonical form. - The
out
parameter is provided just for compatibility with Numpy and isn’t actually supported.
Examples
You can use
COO.prod
to multiply an array across any dimension.>>> x = np.add.outer(np.arange(5), np.arange(5)) >>> x # doctest: +NORMALIZE_WHITESPACE array([[0, 1, 2, 3, 4], [1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [3, 4, 5, 6, 7], [4, 5, 6, 7, 8]]) >>> s = COO.from_numpy(x) >>> s2 = s.prod(axis=1) >>> s2.todense() # doctest: +NORMALIZE_WHITESPACE array([ 0, 120, 720, 2520, 6720])
You can also use the
keepdims
argument to keep the dimensions after the reduction.>>> s3 = s.prod(axis=1, keepdims=True) >>> s3.shape (5, 1)
You can pass in an output datatype, if needed.
>>> s4 = s.prod(axis=1, dtype=np.float16) >>> s4.dtype dtype('float16')
By default, this reduces the array down to one number, multiplying along all axes.
>>> s.prod() 0