COO.mean
- COO.mean(axis=None, keepdims=False, dtype=None, out=None)
Compute the mean along the given axes. Uses all axes by default.
- Parameters:
axis (Union[int, Iterable[int]], optional) – The axes along which to compute the mean. 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.ndarray.mean
Equivalent numpy method.
scipy.sparse.coo_matrix.mean
Equivalent Scipy method.
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.mean
to compute the mean of an array across any dimension.>>> from sparse import COO >>> x = np.array([[1, 2, 0, 0], [0, 1, 0, 0]], dtype="i8") >>> s = COO.from_numpy(x) >>> s2 = s.mean(axis=1) >>> s2.todense() array([0.5, 1.5, 0., 0.])
You can also use the
keepdims
argument to keep the dimensions after the mean.>>> s3 = s.mean(axis=0, keepdims=True) >>> s3.shape (1, 4)
You can pass in an output datatype, if needed.
>>> s4 = s.mean(axis=0, dtype=np.float16) >>> s4.dtype dtype('float16')
By default, this reduces the array down to one number, computing the mean along all axes.
>>> s.mean() 0.5