def mean(self, axis=None, dtype=None, out=None): """Compute the arithmetic mean along the specified axis. Args: axis (int or ``None``): Axis along which the sum is computed. If it is ``None``, it computes the average of all the elements. Select from ``{None, 0, 1, -2, -1}``. Returns: cupy.ndarray: Summed array. .. seealso:: :meth:`scipy.sparse.spmatrix.mean` """ util.validateaxis(axis) nRow, nCol = self.shape data = self.data.copy() if axis is None: n = nRow * nCol elif axis in (0, -2): n = nRow else: n = nCol return self._with_data(data / n).sum(axis, dtype, out)
def _min_or_max(self, axis, out, min_or_max, sum_duplicates, non_zero): if out is not None: raise ValueError(("Sparse matrices do not support " "an 'out' parameter.")) util.validateaxis(axis) if axis is None: if 0 in self.shape: raise ValueError("zero-size array to reduction operation") zero = cupy.zeros((), dtype=self.dtype) if self.nnz == 0: return zero if sum_duplicates: self.sum_duplicates() m = min_or_max(self.data) if non_zero: return m if self.nnz != internal.prod(self.shape): if min_or_max is cupy.min: m = cupy.minimum(zero, m) elif min_or_max is cupy.max: m = cupy.maximum(zero, m) else: assert False return m if axis == 0 or axis == 1: return self._min_or_max_axis(axis, min_or_max, sum_duplicates, non_zero) else: raise ValueError("axis out of range")
def _min_or_max(self, axis, out, min_or_max, sum_duplicates, non_zero): if out is not None: raise ValueError(("Sparse matrices do not support " "an 'out' parameter.")) util.validateaxis(axis) if axis == 0 or axis == 1: return self._min_or_max_axis(axis, min_or_max, sum_duplicates, non_zero) else: raise ValueError("axis out of range")
def sum(self, axis=None, dtype=None, out=None): """Sums the matrix elements over a given axis. Args: axis (int or ``None``): Axis along which the sum is comuted. If it is ``None``, it computes the sum of all the elements. Select from ``{None, 0, 1, -2, -1}``. dtype: The type of returned matrix. If it is not specified, type of the array is used. out (cupy.ndarray): Output matrix. Returns: cupy.ndarray: Summed array. .. seealso:: :meth:`scipy.sparse.spmatrix.sum` """ util.validateaxis(axis) # This implementation uses multiplication, though it is not efficient # for some matrix types. These should override this function. m, n = self.shape if axis is None: return self.dot(cupy.ones(n, dtype=self.dtype)).sum(dtype=dtype, out=out) if axis < 0: axis += 2 if axis == 0: ret = self.T.dot(cupy.ones(m, dtype=self.dtype)).reshape(1, n) else: # axis == 1 ret = self.dot(cupy.ones(n, dtype=self.dtype)).reshape(m, 1) if out is not None: if out.shape != ret.shape: raise ValueError('dimensions do not match') out[:] = ret return out elif dtype is not None: return ret.astype(dtype, copy=False) else: return ret
def _arg_min_or_max(self, axis, out, op, compare, sum_duplicates): if out is not None: raise ValueError("Sparse matrices do not support " "an 'out' parameter.") util.validateaxis(axis) if axis is None: if 0 in self.shape: raise ValueError("Can't apply the operation to " "an empty matrix.") if self.nnz == 0: return 0 else: zero = self.dtype.type(0) mat = self.tocoo() if sum_duplicates: mat.sum_duplicates() am = op(mat.data) m = mat.data[am] if compare(m, zero): return mat.row[am] * mat.shape[1] + mat.col[am] else: size = cupy.prod(mat.shape) if size == mat.nnz: return am else: ind = mat.row * mat.shape[1] + mat.col zero_ind = _find_missing_index(ind, size) if m == zero: return min(zero_ind, am) else: return zero_ind return self._arg_min_or_max_axis(axis, op, sum_duplicates)