本文整理汇总了Python中scipy.sparse.isspmatrix_csc方法的典型用法代码示例。如果您正苦于以下问题:Python sparse.isspmatrix_csc方法的具体用法?Python sparse.isspmatrix_csc怎么用?Python sparse.isspmatrix_csc使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.sparse
的用法示例。
在下文中一共展示了sparse.isspmatrix_csc方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: GetPreconditioner
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import isspmatrix_csc [as 别名]
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()
示例2: GetCuthillMcKeePermutation
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import isspmatrix_csc [as 别名]
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
示例3: csc_matvec
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import isspmatrix_csc [as 别名]
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
示例4: csc_matvecs
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import isspmatrix_csc [as 别名]
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
示例5: _check_A
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import isspmatrix_csc [as 别名]
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))
示例6: run
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import isspmatrix_csc [as 别名]
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])
示例7: _getIndx
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import isspmatrix_csc [as 别名]
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
示例8: test_add_dummy_feature_csc
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import isspmatrix_csc [as 别名]
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]])
示例9: test_csc
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import isspmatrix_csc [as 别名]
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))
示例10: test_csr
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import isspmatrix_csc [as 别名]
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))
示例11: test_csc
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import isspmatrix_csc [as 别名]
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))
示例12: _getIndx
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import isspmatrix_csc [as 别名]
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
示例13: _check_A
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import isspmatrix_csc [as 别名]
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))
示例14: test_add_dummy_feature_csc
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import isspmatrix_csc [as 别名]
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]])
示例15: validate_graph
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import isspmatrix_csc [as 别名]
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