本文整理汇总了Python中scipy.sparse.SparseEfficiencyWarning方法的典型用法代码示例。如果您正苦于以下问题:Python sparse.SparseEfficiencyWarning方法的具体用法?Python sparse.SparseEfficiencyWarning怎么用?Python sparse.SparseEfficiencyWarning使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.sparse
的用法示例。
在下文中一共展示了sparse.SparseEfficiencyWarning方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_example_comparison
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import SparseEfficiencyWarning [as 别名]
def test_example_comparison(self):
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=SparseEfficiencyWarning)
row = array([0,0,1,2,2,2])
col = array([0,2,2,0,1,2])
data = array([1,2,3,-4,5,6])
sM = csr_matrix((data,(row,col)), shape=(3,3), dtype=float)
M = sM.todense()
row = array([0,0,1,1,0,0])
col = array([0,2,1,1,0,0])
data = array([1,1,1,1,1,1])
sN = csr_matrix((data, (row,col)), shape=(3,3), dtype=float)
N = sN.todense()
sX = spsolve(sM, sN)
X = scipy.linalg.solve(M, N)
assert_array_almost_equal(X, sX.todense())
示例2: test_sparse_expm_multiply_interval
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import SparseEfficiencyWarning [as 别名]
def test_sparse_expm_multiply_interval(self):
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=SparseEfficiencyWarning)
np.random.seed(1234)
start = 0.1
stop = 3.2
n = 40
k = 3
endpoint = True
for num in (14, 13, 2):
A = scipy.sparse.rand(n, n, density=0.05)
B = np.random.randn(n, k)
v = np.random.randn(n)
for target in (B, v):
X = expm_multiply(A, target,
start=start, stop=stop, num=num, endpoint=endpoint)
samples = np.linspace(start=start, stop=stop,
num=num, endpoint=endpoint)
for solution, t in zip(X, samples):
assert_allclose(solution,
scipy.linalg.expm(t*A).dot(target))
示例3: test_sparse_expm_multiply
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import SparseEfficiencyWarning [as 别名]
def test_sparse_expm_multiply(self):
np.random.seed(1234)
n = 40
k = 3
nsamples = 10
for i in range(nsamples):
A = scipy.sparse.rand(n, n, density=0.05)
B = np.random.randn(n, k)
observed = expm_multiply(A, B)
with suppress_warnings() as sup:
sup.filter(SparseEfficiencyWarning,
"splu requires CSC matrix format")
sup.filter(SparseEfficiencyWarning,
"spsolve is more efficient when sparse b is in the CSC matrix format")
expected = scipy.linalg.expm(A).dot(B)
assert_allclose(observed, expected)
示例4: test_sparse_expm_multiply_interval
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import SparseEfficiencyWarning [as 别名]
def test_sparse_expm_multiply_interval(self):
np.random.seed(1234)
start = 0.1
stop = 3.2
n = 40
k = 3
endpoint = True
for num in (14, 13, 2):
A = scipy.sparse.rand(n, n, density=0.05)
B = np.random.randn(n, k)
v = np.random.randn(n)
for target in (B, v):
X = expm_multiply(A, target,
start=start, stop=stop, num=num, endpoint=endpoint)
samples = np.linspace(start=start, stop=stop,
num=num, endpoint=endpoint)
with suppress_warnings() as sup:
sup.filter(SparseEfficiencyWarning,
"splu requires CSC matrix format")
sup.filter(SparseEfficiencyWarning,
"spsolve is more efficient when sparse b is in the CSC matrix format")
for solution, t in zip(X, samples):
assert_allclose(solution,
scipy.linalg.expm(t*A).dot(target))
示例5: test_non_square
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import SparseEfficiencyWarning [as 别名]
def test_non_square(self):
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=SparseEfficiencyWarning)
# A is not square.
A = ones((3, 4))
b = ones((4, 1))
assert_raises(ValueError, spsolve, A, b)
# A2 and b2 have incompatible shapes.
A2 = csc_matrix(eye(3))
b2 = array([1.0, 2.0])
assert_raises(ValueError, spsolve, A2, b2)
示例6: test_splu_smoketest
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import SparseEfficiencyWarning [as 别名]
def test_splu_smoketest(self):
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=SparseEfficiencyWarning)
# Check that splu works at all
x = random.rand(self.n)
lu = splu(self.A)
r = self.A*lu.solve(x)
assert_(abs(x - r).max() < 1e-13)
示例7: test_spilu_smoketest
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import SparseEfficiencyWarning [as 别名]
def test_spilu_smoketest(self):
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=SparseEfficiencyWarning)
# Check that spilu works at all
x = random.rand(self.n)
lu = spilu(self.A, drop_tol=1e-2, fill_factor=5)
r = self.A*lu.solve(x)
assert_(abs(x - r).max() < 1e-2)
assert_(abs(x - r).max() > 1e-5)
示例8: test_leftright_precond
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import SparseEfficiencyWarning [as 别名]
def test_leftright_precond(self):
"""Check that QMR works with left and right preconditioners"""
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=SparseEfficiencyWarning)
from scipy.sparse.linalg.dsolve import splu
from scipy.sparse.linalg.interface import LinearOperator
n = 100
dat = ones(n)
A = spdiags([-2*dat, 4*dat, -dat], [-1,0,1],n,n)
b = arange(n,dtype='d')
L = spdiags([-dat/2, dat], [-1,0], n, n)
U = spdiags([4*dat, -dat], [0,1], n, n)
L_solver = splu(L)
U_solver = splu(U)
def L_solve(b):
return L_solver.solve(b)
def U_solve(b):
return U_solver.solve(b)
def LT_solve(b):
return L_solver.solve(b,'T')
def UT_solve(b):
return U_solver.solve(b,'T')
M1 = LinearOperator((n,n), matvec=L_solve, rmatvec=LT_solve)
M2 = LinearOperator((n,n), matvec=U_solve, rmatvec=UT_solve)
x,info = qmr(A, b, tol=1e-8, maxiter=15, M1=M1, M2=M2)
assert_equal(info,0)
assert_normclose(A*x, b, tol=1e-8)
示例9: test_padecases_dtype_sparse_complex
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import SparseEfficiencyWarning [as 别名]
def test_padecases_dtype_sparse_complex(self):
# float32 and complex64 lead to errors in spsolve/UMFpack
dtype = np.complex128
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=SparseEfficiencyWarning)
for scale in [1e-2, 1e-1, 5e-1, 1, 10]:
a = scale * speye(3, 3, dtype=dtype, format='csc')
e = exp(scale) * eye(3, dtype=dtype)
assert_array_almost_equal_nulp(expm(a).toarray(), e, nulp=100)
示例10: test_sparse_expm_multiply
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import SparseEfficiencyWarning [as 别名]
def test_sparse_expm_multiply(self):
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=SparseEfficiencyWarning)
np.random.seed(1234)
n = 40
k = 3
nsamples = 10
for i in range(nsamples):
A = scipy.sparse.rand(n, n, density=0.05)
B = np.random.randn(n, k)
observed = expm_multiply(A, B)
expected = scipy.linalg.expm(A).dot(B)
assert_allclose(observed, expected)
示例11: test_randomized_svd_sparse_warnings
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import SparseEfficiencyWarning [as 别名]
def test_randomized_svd_sparse_warnings():
# randomized_svd throws a warning for lil and dok matrix
rng = np.random.RandomState(42)
X = make_low_rank_matrix(50, 20, effective_rank=10, random_state=rng)
n_components = 5
for cls in (sparse.lil_matrix, sparse.dok_matrix):
X = cls(X)
assert_warns_message(
sparse.SparseEfficiencyWarning,
"Calculating SVD of a {} is expensive. "
"csr_matrix is more efficient.".format(cls.__name__),
randomized_svd, X, n_components, n_iter=1,
power_iteration_normalizer='none')
示例12: test_leftright_precond
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import SparseEfficiencyWarning [as 别名]
def test_leftright_precond(self):
"""Check that QMR works with left and right preconditioners"""
from scipy.sparse.linalg.dsolve import splu
from scipy.sparse.linalg.interface import LinearOperator
n = 100
dat = ones(n)
A = spdiags([-2*dat, 4*dat, -dat], [-1,0,1],n,n)
b = arange(n,dtype='d')
L = spdiags([-dat/2, dat], [-1,0], n, n)
U = spdiags([4*dat, -dat], [0,1], n, n)
with suppress_warnings() as sup:
sup.filter(SparseEfficiencyWarning, "splu requires CSC matrix format")
L_solver = splu(L)
U_solver = splu(U)
def L_solve(b):
return L_solver.solve(b)
def U_solve(b):
return U_solver.solve(b)
def LT_solve(b):
return L_solver.solve(b,'T')
def UT_solve(b):
return U_solver.solve(b,'T')
M1 = LinearOperator((n,n), matvec=L_solve, rmatvec=LT_solve)
M2 = LinearOperator((n,n), matvec=U_solve, rmatvec=UT_solve)
with suppress_warnings() as sup:
sup.filter(DeprecationWarning, ".*called without specifying.*")
x,info = qmr(A, b, tol=1e-8, maxiter=15, M1=M1, M2=M2)
assert_equal(info,0)
assert_normclose(A*x, b, tol=1e-8)
示例13: test_padecases_dtype_sparse_float
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import SparseEfficiencyWarning [as 别名]
def test_padecases_dtype_sparse_float(self):
# float32 and complex64 lead to errors in spsolve/UMFpack
dtype = np.float64
for scale in [1e-2, 1e-1, 5e-1, 1, 10]:
a = scale * speye(3, 3, dtype=dtype, format='csc')
e = exp(scale) * eye(3, dtype=dtype)
with suppress_warnings() as sup:
sup.filter(SparseEfficiencyWarning,
"Changing the sparsity structure of a csc_matrix is expensive.")
exact_onenorm = _expm(a, use_exact_onenorm=True).toarray()
inexact_onenorm = _expm(a, use_exact_onenorm=False).toarray()
assert_array_almost_equal_nulp(exact_onenorm, e, nulp=100)
assert_array_almost_equal_nulp(inexact_onenorm, e, nulp=100)
示例14: test_padecases_dtype_sparse_complex
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import SparseEfficiencyWarning [as 别名]
def test_padecases_dtype_sparse_complex(self):
# float32 and complex64 lead to errors in spsolve/UMFpack
dtype = np.complex128
for scale in [1e-2, 1e-1, 5e-1, 1, 10]:
a = scale * speye(3, 3, dtype=dtype, format='csc')
e = exp(scale) * eye(3, dtype=dtype)
with suppress_warnings() as sup:
sup.filter(SparseEfficiencyWarning,
"Changing the sparsity structure of a csc_matrix is expensive.")
assert_array_almost_equal_nulp(expm(a).toarray(), e, nulp=100)
示例15: test_diagonalize_interslice_kernels
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import SparseEfficiencyWarning [as 别名]
def test_diagonalize_interslice_kernels():
n = 15
m = 8
kernels = [np.arange(n**2).reshape(n,n) + i*n**2 for i in range(m)]
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=sparse.SparseEfficiencyWarning)
K = m_phate.kernel._diagonalize_interslice_kernels(kernels, method='csr')
D = m_phate.kernel._diagonalize_interslice_kernels(kernels, method='dia')
assert (D.tocsr() - K).nnz == 0