本文整理汇总了Python中scipy.sparse.csc_matrix方法的典型用法代码示例。如果您正苦于以下问题:Python sparse.csc_matrix方法的具体用法?Python sparse.csc_matrix怎么用?Python sparse.csc_matrix使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.sparse
的用法示例。
在下文中一共展示了sparse.csc_matrix方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: fit
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import csc_matrix [as 别名]
def fit(self, X, y=None):
mean = y.mean()
lower = mean - self.margin
upper = mean + self.margin
ys = sparse.csc_matrix(y[:, np.newaxis])
if self.weighted:
x = X.multiply(ys).sum(axis=0)
x = x / X.sum(axis=0)
else:
x = (X > 0)
s = x.sum(axis=0)
x = x.multiply(ys).sum(axis=0) / s
x = np.array(x).flatten().astype('f4')
mask1 = (x < lower)
mask2 = (x > upper)
self.mask = (mask1 + mask2).astype(bool)
return self
示例2: load_matlab_file
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import csc_matrix [as 别名]
def load_matlab_file(path_file, name_field):
"""
load '.mat' files
inputs:
path_file, string containing the file path
name_field, string containig the field name (default='shape')
warning:
'.mat' files should be saved in the '-v7.3' format
"""
db = h5py.File(path_file, 'r')
ds = db[name_field]
try:
if 'ir' in ds.keys():
data = np.asarray(ds['data'])
ir = np.asarray(ds['ir'])
jc = np.asarray(ds['jc'])
out = sp.csc_matrix((data, ir, jc)).astype(np.float32)
except AttributeError:
# Transpose in case is a dense matrix because of the row- vs column- major ordering between python and matlab
out = np.asarray(ds).astype(np.float32).T
db.close()
return out
示例3: prepare_solver
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import csc_matrix [as 别名]
def prepare_solver(self):
'''Prepares the object to solve FEM systems
Note
-------
After running this method, do NOT change any attributes of the class!
'''
logger.info(f'Using solver options: {self._solver_options}')
A = sparse.csc_matrix(self.A, copy=True)
A.sort_indices()
dof_map = copy.deepcopy(self.dof_map)
if self.dirichlet is not None:
A, dof_map = self.dirichlet.apply_to_matrix(A, dof_map)
if self._solver_options == 'pardiso':
self._solver = pardiso.Solver(A)
else:
self._A_reduced = A # We need to save this as PETSc does not copy the vectors
self._solver = petsc_solver.Solver(self._solver_options, A)
示例4: create_matrix
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import csc_matrix [as 别名]
def create_matrix(dim, alpha=0.95, smallest_coef=0.1, largest_coef=.9):
''' Based o scikit-learn make_sparse_spd_matrix'''
chol = -np.eye(dim)
aux = np.random.rand(dim, dim)
aux[aux < alpha] = 0
aux[aux > alpha] = (smallest_coef
+ (largest_coef - smallest_coef)
* np.random.rand(np.sum(aux > alpha)))
aux = np.tril(aux, k=-1)
# Permute the lines: we don't want to have asymmetries in the final
# SPD matrix
permutation = np.random.permutation(dim)
aux = aux[permutation].T[permutation]
chol += aux
A = sp.csc_matrix(np.dot(chol.T, chol))
x = np.random.rand(dim)
b = A.dot(x)
return A,b,x
示例5: _read_hb_data
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import csc_matrix [as 别名]
def _read_hb_data(content, header):
# XXX: look at a way to reduce memory here (big string creation)
ptr_string = "".join([content.read(header.pointer_nbytes_full),
content.readline()])
ptr = np.fromstring(ptr_string,
dtype=int, sep=' ')
ind_string = "".join([content.read(header.indices_nbytes_full),
content.readline()])
ind = np.fromstring(ind_string,
dtype=int, sep=' ')
val_string = "".join([content.read(header.values_nbytes_full),
content.readline()])
val = np.fromstring(val_string,
dtype=header.values_dtype, sep=' ')
try:
return csc_matrix((val, ind-1, ptr-1),
shape=(header.nrows, header.ncols))
except ValueError as e:
raise e
示例6: adult_dataload
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import csc_matrix [as 别名]
def adult_dataload():
from scipy.sparse import csc_matrix
import scipy.io as sio
import numpy as np
filepath='D:/PostDocWork/LSML/RandomFourierFeatures/Datasets/adult/adult/adult123.mat'
adult=sio.loadmat(filepath)
Dummy=adult['XTrain']
TrainData=csc_matrix(Dummy,shape=Dummy.shape).toarray()
TrainData=TrainData.T
train_label=np.squeeze(adult['yTrain'])
Dummy1=adult['XTest']
TestData = csc_matrix(Dummy1,shape=Dummy1.shape).toarray()
TestData=TestData.T
test_label = np.squeeze(adult['yTest'])
del Dummy, Dummy1
return TrainData, train_label, TestData, test_label
示例7: ijcnn1_dataload
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import csc_matrix [as 别名]
def ijcnn1_dataload():
from scipy.sparse import csc_matrix
import scipy.io as sio
import numpy as np
filepath='D:\PostDocWork\LSML\RandomFourierFeatures\Datasets\ijcnn1\ijcnn1_combined.mat'
adult=sio.loadmat(filepath)
Dummy=adult['Xtrain']
TrainData=csc_matrix(Dummy,shape=Dummy.shape).toarray()
train_label=np.squeeze(adult['ytrain'])
Dummy1=adult['Xtest']
TestData = csc_matrix(Dummy1,shape=Dummy1.shape).toarray()
test_label = np.squeeze(adult['ytest'])
Dummy2=adult['Xval']
ValData = csc_matrix(Dummy2,shape=Dummy2.shape).toarray()
val_label = np.squeeze(adult['yval'])
return TrainData, train_label, TestData, test_label, ValData, val_label
示例8: census_dataload
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import csc_matrix [as 别名]
def census_dataload():
from scipy.sparse import csc_matrix
import scipy.io as sio
import numpy as np
filepath='D:/PostDocWork/LSML/RandomFourierFeatures/Datasets/census/census/census.mat'
adult=sio.loadmat(filepath)
Dummy=adult['Xtrain']
TrainData=csc_matrix(Dummy,shape=Dummy.shape).toarray()
TrainData=TrainData.T
train_label=np.squeeze(adult['ytrain'])
Dummy1=adult['Xtest']
TestData = csc_matrix(Dummy1,shape=Dummy1.shape).toarray()
TestData=TestData.T
test_label = np.squeeze(adult['ytest'])
del Dummy, Dummy1
return TrainData, train_label, TestData, test_label
示例9: cpu_dataload
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import csc_matrix [as 别名]
def cpu_dataload():
from scipy.sparse import csc_matrix
import scipy.io as sio
import numpy as np
filepath='D:/PostDocWork/LSML/RandomFourierFeatures/Datasets/cpu/cpu/cpu.mat'
adult=sio.loadmat(filepath)
Dummy=adult['Xtrain']
TrainData=csc_matrix(Dummy,shape=Dummy.shape).toarray()
TrainData=TrainData.T
train_label=np.squeeze(adult['ytrain'])
Dummy1=adult['Xtest']
TestData = csc_matrix(Dummy1,shape=Dummy1.shape).toarray()
TestData=TestData.T
test_label = np.squeeze(adult['ytest'])
del Dummy, Dummy1
return TrainData, train_label, TestData, test_label
示例10: test_local_csm_properties_csm
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import csc_matrix [as 别名]
def test_local_csm_properties_csm():
data = tensor.vector()
indices, indptr, shape = (tensor.ivector(), tensor.ivector(),
tensor.ivector())
mode = theano.compile.mode.get_default_mode()
mode = mode.including("specialize", "local_csm_properties_csm")
for CS, cast in [(sparse.CSC, sp.csc_matrix),
(sparse.CSR, sp.csr_matrix)]:
f = theano.function([data, indices, indptr, shape],
sparse.csm_properties(
CS(data, indices, indptr, shape)),
mode=mode)
assert not any(
isinstance(node.op, (sparse.CSM, sparse.CSMProperties))
for node in f.maker.fgraph.toposort())
v = cast(random_lil((10, 40),
config.floatX, 3))
f(v.data, v.indices, v.indptr, v.shape)
示例11: test_local_csm_grad_c
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import csc_matrix [as 别名]
def test_local_csm_grad_c():
raise SkipTest("Opt disabled as it don't support unsorted indices")
if not theano.config.cxx:
raise SkipTest("G++ not available, so we need to skip this test.")
data = tensor.vector()
indices, indptr, shape = (tensor.ivector(), tensor.ivector(),
tensor.ivector())
mode = theano.compile.mode.get_default_mode()
if theano.config.mode == 'FAST_COMPILE':
mode = theano.compile.Mode(linker='c|py', optimizer='fast_compile')
mode = mode.including("specialize", "local_csm_grad_c")
for CS, cast in [(sparse.CSC, sp.csc_matrix), (sparse.CSR, sp.csr_matrix)]:
cost = tensor.sum(sparse.DenseFromSparse()(CS(data, indices, indptr, shape)))
f = theano.function(
[data, indices, indptr, shape],
tensor.grad(cost, data),
mode=mode)
assert not any(isinstance(node.op, sparse.CSMGrad) for node
in f.maker.fgraph.toposort())
v = cast(random_lil((10, 40),
config.floatX, 3))
f(v.data, v.indices, v.indptr, v.shape)
示例12: test_equality_case
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import csc_matrix [as 别名]
def test_equality_case(self):
"""
Test assuring normal behaviour when values
in the matrices are equal
"""
scipy_ver = [int(n) for n in scipy.__version__.split('.')[:2]]
if (bool(scipy_ver < [0, 13])):
raise SkipTest("comparison operators need newer release of scipy")
x = sparse.csc_matrix()
y = theano.tensor.matrix()
m1 = sp.csc_matrix((2, 2), dtype=theano.config.floatX)
m2 = numpy.asarray([[0, 0], [0, 0]], dtype=theano.config.floatX)
for func in self.testsDic:
op = func(y, x)
f = theano.function([y, x], op)
self.assertTrue(numpy.array_equal(f(m2, m1),
self.testsDic[func](m2, m1)))
示例13: test_csm_properties_grad
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import csc_matrix [as 别名]
def test_csm_properties_grad(self):
sp_types = {'csc': sp.csc_matrix,
'csr': sp.csr_matrix}
for format in ['csc', 'csr']:
for dtype in ['float32', 'float64']:
spmat = sp_types[format](random_lil((4, 3), dtype, 3))
verify_grad_sparse(lambda *x: CSMProperties()(*x)[0], [spmat],
structured=True)
verify_grad_sparse(lambda *x: CSMProperties()(*x)[1], [spmat],
structured=True)
verify_grad_sparse(lambda *x: CSMProperties()(*x)[2], [spmat],
structured=True)
verify_grad_sparse(lambda *x: CSMProperties()(*x)[2], [spmat],
structured=True)
示例14: test_csm_unsorted
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import csc_matrix [as 别名]
def test_csm_unsorted(self):
"""
Test support for gradients of unsorted inputs.
"""
sp_types = {'csc': sp.csc_matrix,
'csr': sp.csr_matrix}
for format in ['csr', 'csc', ]:
for dtype in ['float32', 'float64']:
x = tensor.tensor(dtype=dtype, broadcastable=(False,))
y = tensor.ivector()
z = tensor.ivector()
s = tensor.ivector()
# Sparse advanced indexing produces unsorted sparse matrices
a = sparse_random_inputs(format, (4, 3), out_dtype=dtype,
unsorted_indices=True)[1][0]
# Make sure it's unsorted
assert not a.has_sorted_indices
def my_op(x):
y = tensor.constant(a.indices)
z = tensor.constant(a.indptr)
s = tensor.constant(a.shape)
return tensor.sum(
dense_from_sparse(CSM(format)(x, y, z, s) * a))
verify_grad_sparse(my_op, [a.data])
示例15: test_csm
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import csc_matrix [as 别名]
def test_csm(self):
sp_types = {'csc': sp.csc_matrix,
'csr': sp.csr_matrix}
for format in ['csc', 'csr']:
for dtype in ['float32', 'float64']:
x = tensor.tensor(dtype=dtype, broadcastable=(False,))
y = tensor.ivector()
z = tensor.ivector()
s = tensor.ivector()
f = theano.function([x, y, z, s], CSM(format)(x, y, z, s))
spmat = sp_types[format](random_lil((4, 3), dtype, 3))
res = f(spmat.data, spmat.indices, spmat.indptr,
numpy.asarray(spmat.shape, 'int32'))
assert numpy.all(res.data == spmat.data)
assert numpy.all(res.indices == spmat.indices)
assert numpy.all(res.indptr == spmat.indptr)
assert numpy.all(res.shape == spmat.shape)