我正在尝试将稀疏稀疏矩阵与值列表相乘。该代码按预期工作,但我在inv
电话中收到这两个警告:
1)SparseEfficiencyWarning:splu需要CSC矩阵格式警告(“ splu需要CSC矩阵格式”,SparseEfficiencyWarning)
2)SparseEfficiencyWarning:当稀疏b为CSC矩阵格式时,spsolve效率更高warn('当稀疏b时spsolve效率更高')
L_plus_I矩阵是的类型<class 'scipy.sparse.csr.csr_matrix'>
。
from scipy.sparse.linalg import inv
from scipy.sparse import identity
import networkx as nx
no_of_nodes = len(g.nodes)
values = list(nx.get_node_attributes(g, 'value').values())
Laplace = nx.laplacian_matrix(g)
Identity = identity(no_of_nodes)
L_plus_I = Laplace + Identity
Inverse = inv(L_plus_I)
solutions = Inverse * values
很明显这些是因为values
只是一个列表而生成的,但是当我尝试使其稀疏时,会在计算中出错。一般而言,如何改善运行时间?
从另一个问题的矩阵开始:
In [74]: A_sp
Out[74]:
<5x5 sparse matrix of type '<class 'numpy.int64'>'
with 24 stored elements in Compressed Sparse Row format>
In [75]: A_sp.A
Out[75]:
array([[7, 4, 2, 9, 0],
[6, 2, 5, 7, 4],
[6, 6, 4, 3, 1],
[5, 6, 5, 1, 2],
[4, 8, 6, 5, 6]])
在inv
你的警告:
In [76]: inv(A_sp)
/usr/local/lib/python3.8/dist-packages/scipy/sparse/linalg/dsolve/linsolve.py:318: SparseEfficiencyWarning: splu requires CSC matrix format
warn('splu requires CSC matrix format', SparseEfficiencyWarning)
/usr/local/lib/python3.8/dist-packages/scipy/sparse/linalg/dsolve/linsolve.py:215: SparseEfficiencyWarning: spsolve is more efficient when sparse b is in the CSC matrix format
warn('spsolve is more efficient when sparse b '
Out[76]:
<5x5 sparse matrix of type '<class 'numpy.float64'>'
with 25 stored elements in Compressed Sparse Row format>
查看代码,我看到inv(A)
的只是`spsolve(A,I)
In [77]: Identity=sparse.identity(5)
In [79]: Identity
Out[79]:
<5x5 sparse matrix of type '<class 'numpy.float64'>'
with 5 stored elements (1 diagonals) in DIAgonal format>
In [80]: spsolve(A_sp, Identity)
/usr/local/lib/python3.8/dist-packages/scipy/sparse/linalg/dsolve/linsolve.py:318: SparseEfficiencyWarning: splu requires CSC matrix format
warn('splu requires CSC matrix format', SparseEfficiencyWarning)
/usr/local/lib/python3.8/dist-packages/scipy/sparse/linalg/dsolve/linsolve.py:215: SparseEfficiencyWarning: spsolve is more efficient when sparse b is in the CSC matrix format
warn('spsolve is more efficient when sparse b '
Out[80]:
<5x5 sparse matrix of type '<class 'numpy.float64'>'
with 25 stored elements in Compressed Sparse Row format>
转换Identity
格式:
In [81]: spsolve(A_sp, Identity.tocsc())
/usr/local/lib/python3.8/dist-packages/scipy/sparse/linalg/dsolve/linsolve.py:318: SparseEfficiencyWarning: splu requires CSC matrix format
warn('splu requires CSC matrix format', SparseEfficiencyWarning)
Out[81]:
<5x5 sparse matrix of type '<class 'numpy.float64'>'
with 25 stored elements in Compressed Sparse Row format>
A
以及转换:
In [82]: spsolve(A_sp.tocsc(), Identity.tocsc())
Out[82]:
<5x5 sparse matrix of type '<class 'numpy.float64'>'
with 25 stored elements in Compressed Sparse Column format>
是的,没有警告:)
In [83]: _.A
Out[83]:
array([[-1.08108108e+00, 4.59459459e-01, 2.97297297e+00,
-2.40540541e+00, -1.58603289e-16],
[ 3.04054054e-02, -1.72297297e-01, 1.35135135e-01,
-9.79729730e-02, 1.25000000e-01],
[ 1.42567568e+00, -4.12162162e-01, -4.10810811e+00,
3.62837838e+00, -2.50000000e-01],
[ 6.21621622e-01, -1.89189189e-01, -1.45945946e+00,
1.10810811e+00, 8.54017711e-17],
[-1.26351351e+00, 4.93243243e-01, 3.16216216e+00,
-2.81756757e+00, 2.50000000e-01]])
针对密集版本进行测试:
In [84]: np.linalg.inv(A_sp.A)
Out[84]:
array([[-1.08108108e+00, 4.59459459e-01, 2.97297297e+00,
-2.40540541e+00, -1.58603289e-16],
[ 3.04054054e-02, -1.72297297e-01, 1.35135135e-01,
-9.79729730e-02, 1.25000000e-01],
[ 1.42567568e+00, -4.12162162e-01, -4.10810811e+00,
3.62837838e+00, -2.50000000e-01],
[ 6.21621622e-01, -1.89189189e-01, -1.45945946e+00,
1.10810811e+00, 8.54017711e-17],
[-1.26351351e+00, 4.93243243e-01, 3.16216216e+00,
-2.81756757e+00, 2.50000000e-01]])
实际上inv
使用I = _ident_like(A)
,所以这就足够了:
In [85]: inv(A_sp.tocsc())
Out[85]:
<5x5 sparse matrix of type '<class 'numpy.float64'>'
with 25 stored elements in Compressed Sparse Column format>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句