我们从Python开源项目中,提取了以下17个代码示例,用于说明如何使用scipy.sparse.spmatrix()。
def scipy_sparse_to_cvx_sparse(x): ''' This function takes as input as SciPy sparse matrix and converts it into a CVX sparse one. Inputs: ------ x : SciPy sparse matrix. Outputs: ------- y : CVX sparse matrix. ''' # --> Check that the input matrix is indeed a scipy sparse matrix. if sparse.issparse(x) is not True: raise ValueError('Input matrix is not a SciPy sparse matrix.') # --> Convert x to COOdinate format. coo = x.tocoo() # --> Create the corresponding cvx sparse matrix. y = spmatrix(coo.data, coo.row.tolist(), coo.col.tolist()) return y
def __init__(self, dim=None, mean=None, precision=None, mean_lin_op=None, support=None): """ :param int dim: dimensionality of the distribution. If None, inferred from mean or precision :param float|np.ndarray|None mean: the mean, float or 1D array. If not supplied on init or call-time, assumed to be 0. If scalar, assumes the mean is the same for all dimensions. :param float|np.ndarray|sp.spmatrix|None precision: the precision matrix. If not supplied on init or at call-time, assumed to be the identity. If 1D, assumes that the diagonal of the precision matrix is passed in; if scalar and CPD dimensionality > 1, assumes that the precision matrix is precision * identity matrix. :param None|IndexOperator mean_lin_op: linear transform operator for the mean parameter, whose is shape is (dim, len(mean_vector)) :param tuple(float) support: Defines the support for the probability distribution. Passed to solver pars updater to prevent the parameter from being set outside the range of the supports. """ mean, precision, const = self._validate_args(dim=dim, mean=mean, precision=precision) self.mean = mean.ravel() if self.dim > 1 else mean self.precision = precision self.hessian_cache = None self.hessian_mean_cache = None self.const = const self.mean_lin_op = mean_lin_op self.support = support
def hessian_wrt_mean(self): """ The Hessian of the multivariate Gaussian w.r.t. its mean, potentially including the linear projection. :return: The Hessian w.r.t. the mean :rtype: float|np.ndarray|sp.spmatrix """ if self.hessian_mean_cache is None: hessian = self.hessian if self.mean_lin_op is not None: if np.isscalar(hessian) and isinstance(self.mean_lin_op, IndexOperator): # index operator preserves diagonality hessian = sp.diags(self.mean_lin_op.rmatvec(hessian * np.ones(self.dim)), 0) elif np.isscalar(hessian): hessian = hessian * np.eye(self.dim) hessian = rmatvec_nd(self.mean_lin_op, hessian) else: hessian = rmatvec_nd(self.mean_lin_op, hessian) self.hessian_mean_cache = hessian return self.hessian_mean_cache
def get_dependent_vars(self, var_idx): """ Given the indices of the query variables, var_idx, returns the set of dependent variables (including var_idx); i.e., the smallest set S containing var_idx for which (complement of S) indep of (S). This is done by finding all non-zero columns of the `var_idx` rows of the precision matrix. :param np.ndarray[int]|np.ndarray[bool] var_idx: indices of the query variables :return: indices of the dependent variables :rtype: np.ndarray[int] """ if isinstance(self.precision, sp.spmatrix): prec = self.precision.tocsr() elif np.isscalar(self.precision): return var_idx else: prec = self.precision return np.unique(np.nonzero(prec[var_idx, :])[1])
def rmatvec_nd(lin_op, x): """ Project a 1D or 2D numpy or sparse array using rmatvec. This is different from rmatvec because it applies rmatvec to each row and column. If x is n x n and lin_op is n x k, the result will be k x k. :param LinearOperator lin_op: The linear operator to apply to x :param np.ndarray|sp.spmatrix x: array/matrix to be projected :return: the projected array :rtype: np.ndarray|sp.spmatrix """ if x is None or lin_op is None: return x if isinstance(x, sp.spmatrix): y = x.toarray() elif np.isscalar(x): y = np.array(x, ndmin=1) else: y = np.copy(x) proj_func = lambda z: lin_op.rmatvec(z) for j in range(y.ndim): if y.shape[j] == lin_op.shape[0]: y = np.apply_along_axis(proj_func, j, y) return y
def convert(self, x): if (isinstance(x, (np.ndarray, sp.spmatrix))): return sla.aslinearoperator(x) else: assert(False)
def SpMatrix(self, M, **kwargs): """ A := M """ assert isinstance(M, spp.spmatrix) return op.SpMatrix(self, M, **kwargs)
def __init__(self, backend, M, **kwargs): """ Create a new Sparse Matrix Operator from a concrete sparse matrix. """ super().__init__(backend, **kwargs) assert isinstance(M, spp.spmatrix) self._matrix = M self._matrix_d = None self._allow_exwrite = True self._use_dia = False
def numpy_to_cvxopt_matrix(A): ''' This function takes as input a numpy/SciPy array/matrix and converts it to a CVX format. Inputs: ------ A : NumPy/SciPy array or matrix. Outputs: ------- A : Corresponding matrix in CVX format. ''' # --> None case. if A is None: return None # --> sparse SciPy case. if sparse.issparse(A): if isinstance(A, sparse.spmatrix): return scipy_sparse_to_spmatrix(A) else: return A else: # --> Dense matrix or NumPy array. if isinstance(A, np.ndarray): if A.ndim == 1: return matrix(A, (A.shape[0], 1), 'd') else: return matrix(A, A.shape, 'd') else: return A
def hessian(self): """ The Hessian of the multivariate Gaussian :return: The Hessian of the Gaussian distribution. :rtype: float|np.ndarray|sp.spmatrix """ if self.hessian_cache is None: self.hessian_cache = -self.precision return self.hessian_cache
def densify(x): """ Convert sparse matrix or float to a numpy array. If passed numpy array, return same. :param sp.spmatrix|np.ndarray|float x: :return: x as a numpy array :rtype: np.ndarray """ return x.toarray() if sp.issparse(x) else np.array(x, ndmin=2)
def X(self): """Data matrix of shape `n_obs` × `n_vars` (`np.ndarray`, `sp.sparse.spmatrix`).""" if self.isbacked: if not self.file.isopen: self.file.open() X = self.file._file['X'] if self.isview: return X[self._oidx, self._vidx] else: return X else: return self._X
def _slice_uns_sparse_matrices_inplace(self, uns, oidx): # slice sparse spatrices of n_obs × n_obs in self.uns if not (isinstance(oidx, slice) and oidx.start is None and oidx.step is None and oidx.stop is None): for k, v in uns.items(): if isinstance(v, sparse.spmatrix) and v.shape == ( self._n_obs, self._n_obs): uns[k] = v.tocsc()[:, obs].tocsr()[oidx, :]
def _fit_binary(self, X, y): p = np.asarray(self.alpha + X[y == 1].sum(axis=0)).flatten() q = np.asarray(self.alpha + X[y == 0].sum(axis=0)).flatten() r = np.log(p/np.abs(p).sum()) - np.log(q/np.abs(q).sum()) b = np.log((y == 1).sum()) - np.log((y == 0).sum()) if isinstance(X, spmatrix): indices = np.arange(len(r)) r_sparse = coo_matrix( (r, (indices, indices)), shape=(len(r), len(r)) ) X_scaled = X * r_sparse else: X_scaled = X * r lsvc = LinearSVC( C=self.C, fit_intercept=self.fit_intercept, max_iter=10000 ).fit(X_scaled, y) mean_mag = np.abs(lsvc.coef_).mean() coef_ = (1 - self.beta) * mean_mag * r + \ self.beta * (r * lsvc.coef_) intercept_ = (1 - self.beta) * mean_mag * b + \ self.beta * lsvc.intercept_ return coef_, intercept_
def _find_clusters_1dir(x, x_in, connectivity, max_step, t_power, ndimage): """Actually call the clustering algorithm""" if connectivity is None: labels, n_labels = ndimage.label(x_in) if x.ndim == 1: # slices clusters = ndimage.find_objects(labels, n_labels) if len(clusters) == 0: sums = list() else: index = list(range(1, n_labels + 1)) if t_power == 1: sums = ndimage.measurements.sum(x, labels, index=index) else: sums = ndimage.measurements.sum(np.sign(x) * np.abs(x) ** t_power, labels, index=index) else: # boolean masks (raveled) clusters = list() sums = np.empty(n_labels) for l in range(1, n_labels + 1): c = labels == l clusters.append(c.ravel()) if t_power == 1: sums[l - 1] = np.sum(x[c]) else: sums[l - 1] = np.sum(np.sign(x[c]) * np.abs(x[c]) ** t_power) else: if x.ndim > 1: raise Exception("Data should be 1D when using a connectivity " "to define clusters.") if isinstance(connectivity, sparse.spmatrix): clusters = _get_components(x_in, connectivity) elif isinstance(connectivity, list): # use temporal adjacency clusters = _get_clusters_st(x_in, connectivity, max_step) else: raise ValueError('Connectivity must be a sparse matrix or list') if t_power == 1: sums = np.array([np.sum(x[c]) for c in clusters]) else: sums = np.array([np.sum(np.sign(x[c]) * np.abs(x[c]) ** t_power) for c in clusters]) return clusters, np.atleast_1d(sums)