本文整理汇总了Python中scipy.sparse.csr_matrix方法的典型用法代码示例。如果您正苦于以下问题:Python sparse.csr_matrix方法的具体用法?Python sparse.csr_matrix怎么用?Python sparse.csr_matrix使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.sparse
的用法示例。
在下文中一共展示了sparse.csr_matrix方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _convert_dict_to_sparse_matrix
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import csr_matrix [as 别名]
def _convert_dict_to_sparse_matrix(self, feature_rows):
if len(feature_rows) == 0:
raise Exception('ERROR: something went wrong, empty features.')
data, row, col = [], [], []
for i, feature_row in enumerate(feature_rows):
if len(feature_row) == 0:
# case of empty feature set for a specific instance
row.append(i)
col.append(0)
data.append(0)
else:
for feature in feature_row:
row.append(i)
col.append(feature)
data.append(feature_row[feature])
shape = (max(row) + 1, self.feature_size)
data_matrix = csr_matrix((data, (row, col)),
shape=shape, dtype=np.float64)
return data_matrix
示例2: __init__
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import csr_matrix [as 别名]
def __init__(self,node_i,node_j,A,rho,dof,name=None,mass='conc',tol=1e-6):
super(Line,self).__init__(1,dof,name)
self._nodes=[node_i,node_j]
#Initialize local CSys
o = [ node_i.x, node_i.y, node_i.z ]
pt1 = [ node_j.x, node_j.y, node_j.z ]
pt2 = [ node_i.x, node_i.y, node_i.z ]
if abs(node_i.x - node_j.x) < tol and abs(node_i.y - node_j.y) < tol:
pt2[0] += 1
else:
pt2[2] += 1
self._local_csys = Cartisian(o, pt1, pt2)
T=np.zeros((12,12))
V=self._local_csys.transform_matrix
T[:3,:3] =T[3:6,3:6]=T[6:9,6:9]=T[9:,9:]= V
self._T=spr.csr_matrix(T)
self._length=((node_i.x - node_j.x)**2 + (node_i.y - node_j.y)**2 + (node_i.z - node_j.z)**2)**0.5
self._mass=rho*A*self.length
示例3: jacobian
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import csr_matrix [as 别名]
def jacobian(self, params, into=None):
params = flattest(params)
n = len(params)
ii = np.arange(n)
(rs,cs,zs) = ([],[],[])
for ((mn,mx), f) in self.pieces_with_default:
if len(ii) == 0: break
k = np.where((params >= mn) & (params <= mx))[0]
if len(k) == 0: continue
kk = ii[k]
j = f.jacobian(params[k])
if j.shape[0] == 1 and j.shape[1] > 1: j = repmat(j, j.shape[1], 1)
(rj,cj,vj) = sps.find(j)
rs.append(kk[rj])
cs.append(kk[cj])
zs.append(vj)
ii = np.delete(ii, k)
params = np.delete(params, k)
(rs,cs,zs) = [np.concatenate(us) if len(us) > 0 else [] for us in (rs,cs,zs)]
dz = sps.csr_matrix((zs, (rs,cs)), shape=(n,n))
return safe_into(into, dz)
示例4: asscipy
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import csr_matrix [as 别名]
def asscipy(self):
"""Returns a ``scipy.sparse.csr.csr_matrix`` object with value copied from this array
Examples
--------
>>> x = mx.nd.sparse.zeros('csr', (2,3))
>>> y = x.asscipy()
>>> type(y)
<type 'scipy.sparse.csr.csr_matrix'>
>>> y
<2x3 sparse matrix of type '<type 'numpy.float32'>'
with 0 stored elements in Compressed Sparse Row format>
"""
data = self.data.asnumpy()
indices = self.indices.asnumpy()
indptr = self.indptr.asnumpy()
if not spsp:
raise ImportError("scipy is not available. \
Please check if the scipy python bindings are installed.")
return spsp.csr_matrix((data, indices, indptr), shape=self.shape, dtype=self.dtype)
# pylint: disable=abstract-method
示例5: test_create_sparse_nd_from_dense
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import csr_matrix [as 别名]
def test_create_sparse_nd_from_dense():
def check_create_from_dns(shape, f, dense_arr, dtype, default_dtype, ctx):
arr = f(dense_arr, dtype=dtype, ctx=ctx)
assert(same(arr.asnumpy(), np.ones(shape)))
assert(arr.dtype == dtype)
assert(arr.context == ctx)
# verify the default dtype inferred from dense arr
arr2 = f(dense_arr)
assert(arr2.dtype == default_dtype)
assert(arr2.context == Context.default_ctx)
shape = rand_shape_2d()
dtype = np.int32
src_dtype = np.float64
ctx = mx.cpu(1)
dense_arrs = [mx.nd.ones(shape, dtype=src_dtype), np.ones(shape, dtype=src_dtype), \
np.ones(shape, dtype=src_dtype).tolist()]
for f in [mx.nd.sparse.csr_matrix, mx.nd.sparse.row_sparse_array]:
for dense_arr in dense_arrs:
default_dtype = dense_arr.dtype if isinstance(dense_arr, (NDArray, np.ndarray)) \
else np.float32
check_create_from_dns(shape, f, dense_arr, dtype, default_dtype, ctx)
示例6: main
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import csr_matrix [as 别名]
def main():
print('create y...')
y = np.random.randint(2, size=N_OBS)
print('create X...')
row = np.random.randint(N_OBS, size=N_VALUE)
col = np.random.randint(N_FEATURE, size=N_VALUE)
data = np.ones(N_VALUE)
X = sparse.csr_matrix((data, (row, col)), dtype=np.int8)
print('train...')
profiler = cProfile.Profile(subcalls=True, builtins=True, timeunit=0.001,)
clf = FTRL(interaction=False)
profiler.enable()
clf.fit(X, y)
profiler.disable()
profiler.print_stats()
p = clf.predict(X)
print('AUC: {:.4f}'.format(auc(y, p)))
assert auc(y, p) > .5
示例7: load_mtx
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import csr_matrix [as 别名]
def load_mtx(dname):
with open(dname + '/matrix.mtx', 'r') as f:
while True:
header = f.readline()
if not header.startswith('%'):
break
header = header.rstrip().split()
n_genes, n_cells = int(header[0]), int(header[1])
data, i, j = [], [], []
for line in f:
fields = line.rstrip().split()
data.append(float(fields[2]))
i.append(int(fields[1])-1)
j.append(int(fields[0])-1)
X = csr_matrix((data, (i, j)), shape=(n_cells, n_genes))
genes = []
with open(dname + '/genes.tsv', 'r') as f:
for line in f:
fields = line.rstrip().split()
genes.append(fields[1])
assert(len(genes) == n_genes)
return X, np.array(genes)
示例8: load_names
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import csr_matrix [as 别名]
def load_names(data_names, norm=True, log1p=False, verbose=True):
# Load datasets.
datasets = []
genes_list = []
n_cells = 0
for name in data_names:
X_i, genes_i = load_data(name)
if norm:
X_i = normalize(X_i, axis=1)
if log1p:
X_i = np.log1p(X_i)
X_i = csr_matrix(X_i)
datasets.append(X_i)
genes_list.append(genes_i)
n_cells += X_i.shape[0]
if verbose:
print('Loaded {} with {} genes and {} cells'.
format(name, X_i.shape[1], X_i.shape[0]))
if verbose:
print('Found {} cells among all datasets'
.format(n_cells))
return datasets, genes_list, n_cells
示例9: comp_aos_csr
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import csr_matrix [as 别名]
def comp_aos_csr(self, coords, tol=1e-8, ram=160e6):
"""
Compute the atomic orbitals for a given set of (Cartesian) coordinates.
The sparse format CSR is used for output and the computation is organized block-wise.
Thence, larger molecules can be tackled right away
coords :: set of Cartesian coordinates
tol :: tolerance for dropping the values
ram :: size of the allowed block (in bytes)
Returns
co2v :: CSR matrix of shape (coordinate, atomic orbital)
"""
from pyscf.nao.m_aos_libnao import aos_libnao
from pyscf import lib
from scipy.sparse import csr_matrix
if not self.init_sv_libnao_orbs : raise RuntimeError('not self.init_sv_libnao')
assert coords.shape[-1] == 3
nc,no = len(coords), self.norbs
bsize = int(min(max(ram / (no*8.0), 1), nc))
co2v = csr_matrix((nc,no))
for s,f in lib.prange(0,nc,bsize):
ca2o = aos_libnao(coords[s:f], no) # compute values of atomic orbitals
ab = np.where(abs(ca2o)>tol)
co2v += csr_matrix((ca2o[ab].reshape(-1), (ab[0]+s, ab[1])), shape=(nc,no))
return co2v
示例10: __init__
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import csr_matrix [as 别名]
def __init__(self, errgen_rep,
mu, eta, m_star, s, unitarypost_data,
unitarypost_indices, unitarypost_indptr):
dim = errgen_rep.dim
self.errgen_rep = errgen_rep
if len(unitarypost_data) > 0: # (nnz > 0)
self.unitary_postfactor = _sps.csr_matrix(
(unitarypost_data, unitarypost_indices,
unitarypost_indptr), shape=(dim, dim))
else:
self.unitary_postfactor = None # no unitary postfactor
self.mu = mu
self.eta = eta
self.m_star = m_star
self.s = s
super(DMOpRep_Lindblad, self).__init__(dim)
示例11: safereal
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import csr_matrix [as 别名]
def safereal(A, inplace=False, check=False):
"""
Returns the real-part of `A` correctly when `A` is either a dense array or
a sparse matrix
"""
if check:
assert(safenorm(A, 'imag') < 1e-6), "Check failed: taking real-part of matrix w/nonzero imaginary part"
if _sps.issparse(A):
if _sps.isspmatrix_csr(A):
if inplace:
ret = _sps.csr_matrix((_np.real(A.data), A.indices, A.indptr), shape=A.shape, dtype='d')
else: # copy
ret = _sps.csr_matrix((_np.real(A.data).copy(), A.indices.copy(),
A.indptr.copy()), shape=A.shape, dtype='d')
ret.eliminate_zeros()
return ret
else:
raise NotImplementedError("safereal() doesn't work with %s matrices yet" % str(type(A)))
else:
return _np.real(A)
示例12: safeimag
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import csr_matrix [as 别名]
def safeimag(A, inplace=False, check=False):
"""
Returns the imaginary-part of `A` correctly when `A` is either a dense array
or a sparse matrix
"""
if check:
assert(safenorm(A, 'real') < 1e-6), "Check failed: taking imag-part of matrix w/nonzero real part"
if _sps.issparse(A):
if _sps.isspmatrix_csr(A):
if inplace:
ret = _sps.csr_matrix((_np.imag(A.data), A.indices, A.indptr), shape=A.shape, dtype='d')
else: # copy
ret = _sps.csr_matrix((_np.imag(A.data).copy(), A.indices.copy(),
A.indptr.copy()), shape=A.shape, dtype='d')
ret.eliminate_zeros()
return ret
else:
raise NotImplementedError("safereal() doesn't work with %s matrices yet" % str(type(A)))
else:
return _np.imag(A)
示例13: test_sparse_lindblad_param
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import csr_matrix [as 别名]
def test_sparse_lindblad_param(self):
#Test sparse Lindblad gates
print("\nGate Test:")
SparseId = sps.identity(4**2,'d','csr')
gate = LindbladDenseOp.from_operation_matrix( np.identity(4**2,'d') )
print("gate Errgen type (should be dense):",type(gate.errorgen.todense()))
self.assertIsInstance(gate.errorgen.todense(), np.ndarray)
sparseOp = LindbladOp.from_operation_matrix( SparseId )
print("spareGate Errgen type (should be sparse):",type(sparseOp.errorgen.tosparse()))
self.assertIsInstance(sparseOp.errorgen.tosparse(), sps.csr_matrix)
self.assertArraysAlmostEqual(gate.errorgen.todense(),sparseOp.errorgen.todense())
perfectG = std2Q_XYICNOT.target_model().operations['Gix'].copy()
noisyG = std2Q_XYICNOT.target_model().operations['Gix'].copy()
noisyG.depolarize(0.9)
Sparse_noisyG = sps.csr_matrix(noisyG,dtype='d')
Sparse_perfectG = sps.csr_matrix(perfectG,dtype='d')
op2 = LindbladDenseOp.from_operation_matrix( noisyG, perfectG )
sparseGate2 = LindbladOp.from_operation_matrix( Sparse_noisyG, Sparse_perfectG )
print("spareGate2 Errgen type (should be sparse):",type(sparseGate2.errorgen.tosparse()))
self.assertIsInstance(sparseGate2.errorgen.tosparse(), sps.csr_matrix)
#print("errgen = \n"); pygsti.tools.print_mx(op2.err_gen,width=4,prec=1)
#print("sparse errgen = \n"); pygsti.tools.print_mx(sparseGate2.err_gen.toarray(),width=4,prec=1)
self.assertArraysAlmostEqual(op2.errorgen.todense(),sparseGate2.errorgen.todense())
示例14: chebyshev_polynomials
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import csr_matrix [as 别名]
def chebyshev_polynomials(adj, k):
"""Calculate Chebyshev polynomials up to order k. Return a list of sparse matrices (tuple representation)."""
print("Calculating Chebyshev polynomials up to order {}...".format(k))
adj_normalized = normalize_adj(adj)
laplacian = sp.eye(adj.shape[0]) - adj_normalized
largest_eigval, _ = eigsh(laplacian, 1, which='LM')
scaled_laplacian = (
2. / largest_eigval[0]) * laplacian - sp.eye(adj.shape[0])
t_k = list()
t_k.append(sp.eye(adj.shape[0]))
t_k.append(scaled_laplacian)
def chebyshev_recurrence(t_k_minus_one, t_k_minus_two, scaled_lap):
s_lap = sp.csr_matrix(scaled_lap, copy=True)
return 2 * s_lap.dot(t_k_minus_one) - t_k_minus_two
for i in range(2, k+1):
t_k.append(chebyshev_recurrence(t_k[-1], t_k[-2], scaled_laplacian))
return sparse_to_tuple(t_k)
示例15: _get_sparse_matrix_from_libsvm
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import csr_matrix [as 别名]
def _get_sparse_matrix_from_libsvm(payload):
pylist = map(lambda x: x.split(' '), payload.split('\n'))
colon = ':'
row = []
col = []
data = []
for row_idx, line in enumerate(pylist):
for item in line:
if colon in item:
col_idx = item.split(colon)[0]
val = item.split(colon)[1]
row.append(row_idx)
col.append(col_idx)
data.append(val)
row = np.array(row)
col = np.array(col).astype(np.int)
data = np.array(data).astype(np.float)
if not (len(row) == len(col) and len(col) == len(data)):
raise RuntimeError("Dimension checking failed when transforming sparse matrix.")
return csr_matrix((data, (row, col)))