Python scipy.sparse.isspmatrix_csc() Examples
The following are 22
code examples of scipy.sparse.isspmatrix_csc().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
scipy.sparse
, or try the search function
.
Example #1
Source File: LinearSolver.py From florence with MIT License | 6 votes |
def GetPreconditioner(self,A, type="amg_smoothed_aggregation"): """Applies a suitable preconditioner to sparse matrix A based on algebraic multigrid of incomplete LU/Cholesky factorisation input: A: [csc_matrix or csc_matrix] type: [str] either "amg_smoothed_aggregation" for a preconditioner based on algebraic multigrid or "incomplete_lu" for scipy's spilu linear operator returns: A preconditioner that can be used in conjunction with scipy's sparse linear iterative solvers (the M keyword in scipy's iterative solver) """ if not (isspmatrix_csc(A) or isspmatrix_csr(A)): raise TypeError("Matrix must be in CSC or CSR sparse format for preconditioning") ml = smoothed_aggregation_solver(A) return ml.aspreconditioner()
Example #2
Source File: LinearSolver.py From florence with MIT License | 6 votes |
def GetCuthillMcKeePermutation(self,A): """Applies Cuthill-Mckee permutation to reduce the sparse matrix bandwidth input: A: [csc_matrix or csr_matrix] returns: perm: [1D array] of permutation such that A[perm,:][:,perm] has its non-zero elements closer to the diagonal """ if not (isspmatrix_csc(A) or isspmatrix_csr(A)): raise TypeError("Matrix must be in CSC or CSR sparse format " "for Cuthill-McKee permutation") if int(sp.__version__.split('.')[1]) >= 15: from scipy.sparse.csgraph import reverse_cuthill_mckee perm = reverse_cuthill_mckee(A) else: from Florence.Tensor import symrcm perm = symrcm(A) return perm
Example #3
Source File: m_sparsetools.py From pyscf with Apache License 2.0 | 5 votes |
def csc_matvecs(csc, B, transB = False, order="C"): """ Matrix matrix multiplication using csc format """ if not sparse.isspmatrix_csc(csc): raise Exception("Matrix must be in csc format") if transB: # Here need to be careful, since using the transpose of B # will change from row major to col major and vice-versa mat = np.require(B.T, dtype=B.dtype, requirements=["A", "O", order]) else: mat = np.require(B, dtype=B.dtype, requirements=["A", "O", order]) nrow, ncol = csc.shape nvecs = mat.shape[1] if csc.dtype == np.float32: C = np.zeros((nrow, nvecs), dtype=np.float32, order=order) libsparsetools.scsc_matvecs(c_int(nrow), c_int(ncol), c_int(nvecs), csc.indptr.ctypes.data_as(POINTER(c_int)), csc.indices.ctypes.data_as(POINTER(c_int)), csc.data.ctypes.data_as(POINTER(c_float)), mat.ctypes.data_as(POINTER(c_float)), C.ctypes.data_as(POINTER(c_float))) elif csc.dtype == np.float64: C = np.zeros((nrow, nvecs), dtype=np.float64, order=order) libsparsetools.dcsc_matvecs(c_int(nrow), c_int(ncol), c_int(nvecs), csc.indptr.ctypes.data_as(POINTER(c_int)), csc.indices.ctypes.data_as(POINTER(c_int)), csc.data.ctypes.data_as(POINTER(c_double)), mat.ctypes.data_as(POINTER(c_double)), C.ctypes.data_as(POINTER(c_double))) else: raise ValueError("Not implemented") return C
Example #4
Source File: test_data.py From twitter-stock-recommendation with MIT License | 5 votes |
def test_add_dummy_feature_csc(): X = sparse.csc_matrix([[1, 0], [0, 1], [0, 1]]) X = add_dummy_feature(X) assert_true(sparse.isspmatrix_csc(X), X) assert_array_equal(X.toarray(), [[1, 1, 0], [1, 0, 1], [1, 0, 1]])
Example #5
Source File: pardiso_wrapper.py From PyPardisoProject with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _check_A(self, A): if A.shape[0] != A.shape[1]: raise ValueError('Matrix A needs to be square, but has shape: {}'.format(A.shape)) if sp.isspmatrix_csr(A): self._solve_transposed = False self.set_iparm(12,0) elif sp.isspmatrix_csc(A): self._solve_transposed = True self.set_iparm(12,1) else: msg = 'PyPardiso requires matrix A to be in CSR or CSC format, but matrix A is: {}'.format(type(A)) raise TypeError(msg) # scipy allows unsorted csr-indices, which lead to completely wrong pardiso results if not A.has_sorted_indices: A.sort_indices() # scipy allows csr matrices with empty rows. a square matrix with an empty row is singular. calling # pardiso with a matrix A that contains empty rows leads to a segfault, same applies for csc with # empty columns if not np.diff(A.indptr).all(): row_col = 'column' if self._solve_transposed else 'row' raise ValueError('Matrix A is singular, because it contains empty {}(s)'.format(row_col)) if A.dtype != np.float64: raise TypeError('PyPardiso currently only supports float64, but matrix A has dtype: {}'.format(A.dtype))
Example #6
Source File: umfpack.py From scikit-umfpack with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _getIndx(self, mtx): if sp.isspmatrix_csc(mtx): indx = mtx.indices self.isCSR = 0 elif sp.isspmatrix_csr(mtx): indx = mtx.indices self.isCSR = 1 else: raise TypeError('must be a CSC/CSR matrix (is %s)' % mtx.__class__) ## # Should check types of indices to correspond to familyTypes. if self.family[1] == 'i': if (indx.dtype != np.dtype(np.int32)) \ or mtx.indptr.dtype != np.dtype(np.int32): raise ValueError('matrix must have int indices') else: if (indx.dtype != np.dtype(np.int64)) \ or mtx.indptr.dtype != np.dtype(np.int64): raise ValueError('matrix must have long indices') if self.isReal: if mtx.data.dtype != np.dtype(np.float64): raise ValueError('matrix must have float64 values') else: if mtx.data.dtype != np.dtype(np.complex128): raise ValueError('matrix must have complex128 values') return indx ## # 30.11.2005, c # last revision: 10.01.2007
Example #7
Source File: test_csc.py From cupy with MIT License | 5 votes |
def test_csc(self): x = sparse.csc_matrix( (cupy.array([], 'f'), cupy.array([], 'i'), cupy.array([0], 'i')), shape=(0, 0), dtype='f') self.assertTrue(sparse.isspmatrix_csc(x))
Example #8
Source File: test_csc.py From cupy with MIT License | 5 votes |
def test_csr(self): x = sparse.csr_matrix( (cupy.array([], 'f'), cupy.array([], 'i'), cupy.array([0], 'i')), shape=(0, 0), dtype='f') self.assertFalse(sparse.isspmatrix_csc(x))
Example #9
Source File: test_csr.py From cupy with MIT License | 5 votes |
def test_csc(self): x = sparse.csr_matrix( (cupy.array([], 'f'), cupy.array([], 'i'), cupy.array([0], 'i')), shape=(0, 0), dtype='f') self.assertFalse(sparse.isspmatrix_csc(x))
Example #10
Source File: test_data.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
def test_add_dummy_feature_csc(): X = sparse.csc_matrix([[1, 0], [0, 1], [0, 1]]) X = add_dummy_feature(X) assert sparse.isspmatrix_csc(X), X assert_array_equal(X.toarray(), [[1, 1, 0], [1, 0, 1], [1, 0, 1]])
Example #11
Source File: umfpack.py From Computable with MIT License | 5 votes |
def _getIndx(self, mtx): if sp.isspmatrix_csc(mtx): indx = mtx.indices self.isCSR = 0 elif sp.isspmatrix_csr(mtx): indx = mtx.indices self.isCSR = 1 else: raise TypeError('must be a CSC/CSR matrix (is %s)' % mtx.__class__) ## # Should check types of indices to correspond to familyTypes. if self.family[1] == 'i': if (indx.dtype != np.dtype('i')) \ or mtx.indptr.dtype != np.dtype('i'): raise ValueError('matrix must have int indices') else: if (indx.dtype != np.dtype('l')) \ or mtx.indptr.dtype != np.dtype('l'): raise ValueError('matrix must have long indices') if self.isReal: if mtx.data.dtype != np.dtype('f8'): raise ValueError('matrix must have float64 values') else: if mtx.data.dtype != np.dtype('c16'): raise ValueError('matrix must have complex128 values') return indx ## # 30.11.2005, c # last revision: 10.01.2007
Example #12
Source File: model_loop.py From fake-news-detection with MIT License | 5 votes |
def run(self): ''' Loads data from csvs, executes basic data checks, runs loop. If roc is not False, will print ROC to the filename specified. ''' # Run Data checks if self.method == 'pandas': self.data_checks(self.raw_X_train) self.data_checks(self.raw_X_test) if self.method == 'csc': if not isspmatrix_csc(self.raw_X_train): self.X_train = csc_matrix(self.raw_X_train) self.X_test = csc_matrix(self.raw_X_test) self.y_test = csc_matrix(self.y_test) self.y_train = csc_matrix(self.y_train) individuals = self.y_test # Generate features parser = spacy.load('en') if self.setting == 'all': params = [(True, False, 'no_tfidf'), (True, True, 'both'), (False, True, 'no_grammar')] if self.setting == 'both_only': params = [(True, True, 'both')] if self.setting == 'grammar_only': params = [(True, False, 'no_tfidf')] if self.setting == 'tfidf_only': params = [(False, True, 'no_grammar')] for params in params: print('Feature generation set to {} for this run'.format(params[2])) f = get_feature_transformer(parser, run_grammar=params[0], run_tfidf=params[1]) self.X_train = f.fit_transform(self.raw_X_train).todense() self.X_test = f.transform(self.raw_X_test).todense() # Run the loop self.clf_loop(self.X_train, self.X_test, self.y_train, self.y_test, individuals, params[2])
Example #13
Source File: pardiso.py From simnibs with GNU General Public License v3.0 | 5 votes |
def _check_A(self, A): if A.shape[0] != A.shape[1]: raise ValueError('Matrix A needs to be square, but has shape: {}'.format(A.shape)) if sp.isspmatrix_csr(A): self._solve_transposed = False self._iparm[11] = 0 elif sp.isspmatrix_csc(A): self._solve_transposed = True self._iparm[11] = 1 else: msg = 'Pardiso requires matrix A to be in CSR or CSC format,' \ ' but matrix A is: {}'.format(type(A)) raise TypeError(msg) # scipy allows unsorted csr-indices, which lead to completely wrong pardiso results if not A.has_sorted_indices: A.sort_indices() # scipy allows csr matrices with empty rows. a square matrix with an empty row is singular. calling # pardiso with a matrix A that contains empty rows leads to a segfault, same applies for csc with # empty columns if not np.diff(A.indptr).all(): row_col = 'column' if self._solve_transposed else 'row' raise ValueError('Matrix A is singular, because it contains empty' ' {}(s)'.format(row_col)) if A.dtype != np.float64: raise TypeError('Pardiso currently only supports float64, ' 'but matrix A has dtype: {}'.format(A.dtype))
Example #14
Source File: m_sparsetools.py From pyscf with Apache License 2.0 | 5 votes |
def csc_matvec(csc, x): """ Matrix vector multiplication using csc format """ if not sparse.isspmatrix_csc(csc): raise Exception("Matrix must be in csc format") nrow, ncol = csc.shape nnz = csc.data.shape[0] if x.size != ncol: print(x.size, ncol) raise ValueError("wrong dimension!") xx = np.require(x, requirements="C") if csc.dtype == np.float32: y = np.zeros((nrow), dtype=np.float32) libsparsetools.scsc_matvec(c_int(nrow), c_int(ncol), c_int(nnz), csc.indptr.ctypes.data_as(POINTER(c_int)), csc.indices.ctypes.data_as(POINTER(c_int)), csc.data.ctypes.data_as(POINTER(c_float)), xx.ctypes.data_as(POINTER(c_float)), y.ctypes.data_as(POINTER(c_float))) elif csc.dtype == np.float64: y = np.zeros((nrow), dtype=np.float64) libsparsetools.dcsc_matvec(c_int(nrow), c_int(ncol), c_int(nnz), csc.indptr.ctypes.data_as(POINTER(c_int)), csc.indices.ctypes.data_as(POINTER(c_int)), csc.data.ctypes.data_as(POINTER(c_double)), xx.ctypes.data_as(POINTER(c_double)), y.ctypes.data_as(POINTER(c_double))) else: raise ValueError("Not implemented") return y
Example #15
Source File: data.py From Mastering-Elasticsearch-7.0 with MIT License | 4 votes |
def add_dummy_feature(X, value=1.0): """Augment dataset with an additional dummy feature. This is useful for fitting an intercept term with implementations which cannot otherwise fit it directly. Parameters ---------- X : {array-like, sparse matrix}, shape [n_samples, n_features] Data. value : float Value to use for the dummy feature. Returns ------- X : {array, sparse matrix}, shape [n_samples, n_features + 1] Same data with dummy feature added as first column. Examples -------- >>> from sklearn.preprocessing import add_dummy_feature >>> add_dummy_feature([[0, 1], [1, 0]]) array([[1., 0., 1.], [1., 1., 0.]]) """ X = check_array(X, accept_sparse=['csc', 'csr', 'coo'], dtype=FLOAT_DTYPES) n_samples, n_features = X.shape shape = (n_samples, n_features + 1) if sparse.issparse(X): if sparse.isspmatrix_coo(X): # Shift columns to the right. col = X.col + 1 # Column indices of dummy feature are 0 everywhere. col = np.concatenate((np.zeros(n_samples), col)) # Row indices of dummy feature are 0, ..., n_samples-1. row = np.concatenate((np.arange(n_samples), X.row)) # Prepend the dummy feature n_samples times. data = np.concatenate((np.full(n_samples, value), X.data)) return sparse.coo_matrix((data, (row, col)), shape) elif sparse.isspmatrix_csc(X): # Shift index pointers since we need to add n_samples elements. indptr = X.indptr + n_samples # indptr[0] must be 0. indptr = np.concatenate((np.array([0]), indptr)) # Row indices of dummy feature are 0, ..., n_samples-1. indices = np.concatenate((np.arange(n_samples), X.indices)) # Prepend the dummy feature n_samples times. data = np.concatenate((np.full(n_samples, value), X.data)) return sparse.csc_matrix((data, indices, indptr), shape) else: klass = X.__class__ return klass(add_dummy_feature(X.tocoo(), value)) else: return np.hstack((np.full((n_samples, 1), value), X))
Example #16
Source File: _validation.py From GraphicDesignPatternByPython with MIT License | 4 votes |
def validate_graph(csgraph, directed, dtype=DTYPE, csr_output=True, dense_output=True, copy_if_dense=False, copy_if_sparse=False, null_value_in=0, null_value_out=np.inf, infinity_null=True, nan_null=True): """Routine for validation and conversion of csgraph inputs""" if not (csr_output or dense_output): raise ValueError("Internal: dense or csr output must be true") # if undirected and csc storage, then transposing in-place # is quicker than later converting to csr. if (not directed) and isspmatrix_csc(csgraph): csgraph = csgraph.T if isspmatrix(csgraph): if csr_output: csgraph = csr_matrix(csgraph, dtype=DTYPE, copy=copy_if_sparse) else: csgraph = csgraph_to_dense(csgraph, null_value=null_value_out) elif np.ma.isMaskedArray(csgraph): if dense_output: mask = csgraph.mask csgraph = np.array(csgraph.data, dtype=DTYPE, copy=copy_if_dense) csgraph[mask] = null_value_out else: csgraph = csgraph_from_masked(csgraph) else: if dense_output: csgraph = csgraph_masked_from_dense(csgraph, copy=copy_if_dense, null_value=null_value_in, nan_null=nan_null, infinity_null=infinity_null) mask = csgraph.mask csgraph = np.asarray(csgraph.data, dtype=DTYPE) csgraph[mask] = null_value_out else: csgraph = csgraph_from_dense(csgraph, null_value=null_value_in, infinity_null=infinity_null, nan_null=nan_null) if csgraph.ndim != 2: raise ValueError("compressed-sparse graph must be two dimensional") if csgraph.shape[0] != csgraph.shape[1]: raise ValueError("compressed-sparse graph must be shape (N, N)") return csgraph
Example #17
Source File: _validation.py From Computable with MIT License | 4 votes |
def validate_graph(csgraph, directed, dtype=DTYPE, csr_output=True, dense_output=True, copy_if_dense=False, copy_if_sparse=False, null_value_in=0, null_value_out=np.inf, infinity_null=True, nan_null=True): """Routine for validation and conversion of csgraph inputs""" if not (csr_output or dense_output): raise ValueError("Internal: dense or csr output must be true") # if undirected and csc storage, then transposing in-place # is quicker than later converting to csr. if (not directed) and isspmatrix_csc(csgraph): csgraph = csgraph.T if isspmatrix(csgraph): if csr_output: csgraph = csr_matrix(csgraph, dtype=DTYPE, copy=copy_if_sparse) else: csgraph = csgraph_to_dense(csgraph, null_value=null_value_out) elif np.ma.is_masked(csgraph): if dense_output: mask = csgraph.mask csgraph = np.array(csgraph.data, dtype=DTYPE, copy=copy_if_dense) csgraph[mask] = null_value_out else: csgraph = csgraph_from_masked(csgraph) else: if dense_output: csgraph = csgraph_masked_from_dense(csgraph, copy=copy_if_dense, null_value=null_value_in, nan_null=nan_null, infinity_null=infinity_null) mask = csgraph.mask csgraph = np.asarray(csgraph.data, dtype=DTYPE) csgraph[mask] = null_value_out else: csgraph = csgraph_from_dense(csgraph, null_value=null_value_in, infinity_null=infinity_null, nan_null=nan_null) if csgraph.ndim != 2: raise ValueError("compressed-sparse graph must be two dimensional") if csgraph.shape[0] != csgraph.shape[1]: raise ValueError("compressed-sparse graph must be shape (N, N)") return csgraph
Example #18
Source File: _validation.py From lambda-packs with MIT License | 4 votes |
def validate_graph(csgraph, directed, dtype=DTYPE, csr_output=True, dense_output=True, copy_if_dense=False, copy_if_sparse=False, null_value_in=0, null_value_out=np.inf, infinity_null=True, nan_null=True): """Routine for validation and conversion of csgraph inputs""" if not (csr_output or dense_output): raise ValueError("Internal: dense or csr output must be true") # if undirected and csc storage, then transposing in-place # is quicker than later converting to csr. if (not directed) and isspmatrix_csc(csgraph): csgraph = csgraph.T if isspmatrix(csgraph): if csr_output: csgraph = csr_matrix(csgraph, dtype=DTYPE, copy=copy_if_sparse) else: csgraph = csgraph_to_dense(csgraph, null_value=null_value_out) elif np.ma.isMaskedArray(csgraph): if dense_output: mask = csgraph.mask csgraph = np.array(csgraph.data, dtype=DTYPE, copy=copy_if_dense) csgraph[mask] = null_value_out else: csgraph = csgraph_from_masked(csgraph) else: if dense_output: csgraph = csgraph_masked_from_dense(csgraph, copy=copy_if_dense, null_value=null_value_in, nan_null=nan_null, infinity_null=infinity_null) mask = csgraph.mask csgraph = np.asarray(csgraph.data, dtype=DTYPE) csgraph[mask] = null_value_out else: csgraph = csgraph_from_dense(csgraph, null_value=null_value_in, infinity_null=infinity_null, nan_null=nan_null) if csgraph.ndim != 2: raise ValueError("compressed-sparse graph must be two dimensional") if csgraph.shape[0] != csgraph.shape[1]: raise ValueError("compressed-sparse graph must be shape (N, N)") return csgraph
Example #19
Source File: data.py From Splunking-Crime with GNU Affero General Public License v3.0 | 4 votes |
def add_dummy_feature(X, value=1.0): """Augment dataset with an additional dummy feature. This is useful for fitting an intercept term with implementations which cannot otherwise fit it directly. Parameters ---------- X : {array-like, sparse matrix}, shape [n_samples, n_features] Data. value : float Value to use for the dummy feature. Returns ------- X : {array, sparse matrix}, shape [n_samples, n_features + 1] Same data with dummy feature added as first column. Examples -------- >>> from sklearn.preprocessing import add_dummy_feature >>> add_dummy_feature([[0, 1], [1, 0]]) array([[ 1., 0., 1.], [ 1., 1., 0.]]) """ X = check_array(X, accept_sparse=['csc', 'csr', 'coo'], dtype=FLOAT_DTYPES) n_samples, n_features = X.shape shape = (n_samples, n_features + 1) if sparse.issparse(X): if sparse.isspmatrix_coo(X): # Shift columns to the right. col = X.col + 1 # Column indices of dummy feature are 0 everywhere. col = np.concatenate((np.zeros(n_samples), col)) # Row indices of dummy feature are 0, ..., n_samples-1. row = np.concatenate((np.arange(n_samples), X.row)) # Prepend the dummy feature n_samples times. data = np.concatenate((np.ones(n_samples) * value, X.data)) return sparse.coo_matrix((data, (row, col)), shape) elif sparse.isspmatrix_csc(X): # Shift index pointers since we need to add n_samples elements. indptr = X.indptr + n_samples # indptr[0] must be 0. indptr = np.concatenate((np.array([0]), indptr)) # Row indices of dummy feature are 0, ..., n_samples-1. indices = np.concatenate((np.arange(n_samples), X.indices)) # Prepend the dummy feature n_samples times. data = np.concatenate((np.ones(n_samples) * value, X.data)) return sparse.csc_matrix((data, indices, indptr), shape) else: klass = X.__class__ return klass(add_dummy_feature(X.tocoo(), value)) else: return np.hstack((np.ones((n_samples, 1)) * value, X))
Example #20
Source File: _validation.py From Splunking-Crime with GNU Affero General Public License v3.0 | 4 votes |
def validate_graph(csgraph, directed, dtype=DTYPE, csr_output=True, dense_output=True, copy_if_dense=False, copy_if_sparse=False, null_value_in=0, null_value_out=np.inf, infinity_null=True, nan_null=True): """Routine for validation and conversion of csgraph inputs""" if not (csr_output or dense_output): raise ValueError("Internal: dense or csr output must be true") # if undirected and csc storage, then transposing in-place # is quicker than later converting to csr. if (not directed) and isspmatrix_csc(csgraph): csgraph = csgraph.T if isspmatrix(csgraph): if csr_output: csgraph = csr_matrix(csgraph, dtype=DTYPE, copy=copy_if_sparse) else: csgraph = csgraph_to_dense(csgraph, null_value=null_value_out) elif np.ma.isMaskedArray(csgraph): if dense_output: mask = csgraph.mask csgraph = np.array(csgraph.data, dtype=DTYPE, copy=copy_if_dense) csgraph[mask] = null_value_out else: csgraph = csgraph_from_masked(csgraph) else: if dense_output: csgraph = csgraph_masked_from_dense(csgraph, copy=copy_if_dense, null_value=null_value_in, nan_null=nan_null, infinity_null=infinity_null) mask = csgraph.mask csgraph = np.asarray(csgraph.data, dtype=DTYPE) csgraph[mask] = null_value_out else: csgraph = csgraph_from_dense(csgraph, null_value=null_value_in, infinity_null=infinity_null, nan_null=nan_null) if csgraph.ndim != 2: raise ValueError("compressed-sparse graph must be two dimensional") if csgraph.shape[0] != csgraph.shape[1]: raise ValueError("compressed-sparse graph must be shape (N, N)") return csgraph
Example #21
Source File: scipy_aliases.py From PyPardisoProject with BSD 3-Clause "New" or "Revised" License | 4 votes |
def spsolve(A, b, factorize=True, squeeze=True, solver=pypardiso_solver, *args, **kwargs): """ This function mimics scipy.sparse.linalg.spsolve, but uses the Pardiso solver instead of SuperLU/UMFPACK solve Ax=b for x --- Parameters --- A: sparse square CSR or CSC matrix (scipy.sparse.csr.csr_matrix) b: numpy ndarray right-hand side(s), b.shape[0] needs to be the same as A.shape[0] factorize: boolean, default True matrix A is factorized by default, so the factorization can be reused squeeze: default True strange quirk of scipy spsolve, which always returns x.squeeze(), this feature in order to keep it compatible with implementations that rely on this behaviour solver: instance of PyPardisoSolver, default pypardiso_solver you can supply your own instance of PyPardisoSolver, but using several instances of PyPardisoSolver in parallel can lead to errors --- Returns --- x: numpy ndarray solution of the system of linear equations, same shape as b (but returnsshape (n,) if b has shape (n,1)) --- Notes --- The computation time increases only minimally if the factorization and the solve phase are carried out in two steps, therefore it is factorized by default. Subsequent calls to spsolve with the same matrix A will be drastically faster. This makes the "factorized" method obsolete, but it is kept for compatibility. """ if sp.isspmatrix_csc(A): A = A.tocsr() # fixes issue with brightway2 technosphere matrix solver._check_A(A) if factorize and not solver._is_already_factorized(A): solver.factorize(A) x = solver.solve(A, b) if squeeze: return x.squeeze() # scipy spsolve always returns vectors with shape (n,) indstead of (n,1) else: return x
Example #22
Source File: data.py From twitter-stock-recommendation with MIT License | 4 votes |
def add_dummy_feature(X, value=1.0): """Augment dataset with an additional dummy feature. This is useful for fitting an intercept term with implementations which cannot otherwise fit it directly. Parameters ---------- X : {array-like, sparse matrix}, shape [n_samples, n_features] Data. value : float Value to use for the dummy feature. Returns ------- X : {array, sparse matrix}, shape [n_samples, n_features + 1] Same data with dummy feature added as first column. Examples -------- >>> from sklearn.preprocessing import add_dummy_feature >>> add_dummy_feature([[0, 1], [1, 0]]) array([[ 1., 0., 1.], [ 1., 1., 0.]]) """ X = check_array(X, accept_sparse=['csc', 'csr', 'coo'], dtype=FLOAT_DTYPES) n_samples, n_features = X.shape shape = (n_samples, n_features + 1) if sparse.issparse(X): if sparse.isspmatrix_coo(X): # Shift columns to the right. col = X.col + 1 # Column indices of dummy feature are 0 everywhere. col = np.concatenate((np.zeros(n_samples), col)) # Row indices of dummy feature are 0, ..., n_samples-1. row = np.concatenate((np.arange(n_samples), X.row)) # Prepend the dummy feature n_samples times. data = np.concatenate((np.ones(n_samples) * value, X.data)) return sparse.coo_matrix((data, (row, col)), shape) elif sparse.isspmatrix_csc(X): # Shift index pointers since we need to add n_samples elements. indptr = X.indptr + n_samples # indptr[0] must be 0. indptr = np.concatenate((np.array([0]), indptr)) # Row indices of dummy feature are 0, ..., n_samples-1. indices = np.concatenate((np.arange(n_samples), X.indices)) # Prepend the dummy feature n_samples times. data = np.concatenate((np.ones(n_samples) * value, X.data)) return sparse.csc_matrix((data, indices, indptr), shape) else: klass = X.__class__ return klass(add_dummy_feature(X.tocoo(), value)) else: return np.hstack((np.ones((n_samples, 1)) * value, X))