Scipy稀疏效率乘以两个矩阵时的警告

leo_bouts

我正在尝试将稀疏稀疏矩阵与值列表相乘。该代码按预期工作,但我在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只是一个列表而生成的,但是当我尝试使其稀疏时,会在计算中出错。一般而言,如何改善运行时间?

hpaulj

从另一个问题的矩阵开始:

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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

当我使用scipy乘以两个CSR矩阵时,为什么会占用这么多内存?

来自分类Dev

两个稀疏矩阵相乘的算法

来自分类Dev

乘以 2 个稀疏矩阵

来自分类Dev

两个矩阵乘以一个向量

来自分类Dev

两个矩阵乘以一个向量

来自分类Dev

两个稀疏矩阵的按元素最大值

来自分类Dev

如何获得两个稀疏矩阵的集合差异?

来自分类Dev

使用numpy.dot乘以两个巨大的矩阵

来自分类Dev

将两个不同大小的矩阵乘以 locb

来自分类Dev

在MATLAB中将两个非常大的稀疏矩阵相乘时出现内存不足错误

来自分类Dev

Hadamard的两个巨大稠密矩阵的乘积与稀疏矩阵的乘积

来自分类Dev

用scipy或sklearn缩放两个矩阵

来自分类Dev

在python中比较两个scipy.sparse矩阵

来自分类Dev

在python中比较两个scipy.sparse矩阵

来自分类Dev

scipy sparse:获取两个矩阵中存在的值

来自分类Dev

仅影响零值的两个稀疏矩阵的点积

来自分类Dev

将spfun与两个相同顺序的稀疏矩阵一起使用

来自分类Dev

在python中添加两个形状不同的`csc`稀疏矩阵

来自分类Dev

在R中合并两个大小不同的dgCMatrix稀疏矩阵

来自分类Dev

在python中添加两个形状不同的`csc`稀疏矩阵

来自分类Dev

tensorflow:如何在两个稀疏矩阵之间执行元素乘法

来自分类Dev

如何有效地逐列乘以两个矩阵

来自分类Dev

tensorflow 两个矩阵元素乘以轴 = 0, (M,k) * (M,l) --> (M,k*l)

来自分类Dev

Matlab-如何提高两个端口矩阵的计算效率?

来自分类Dev

稀疏矩阵乘以密集矩形矩阵

来自分类Dev

NumPy:将每对2d矩阵乘以两个3d矩阵的有效方法吗?

来自分类Dev

将scipy稀疏行矩阵添加到另一个稀疏矩阵

来自分类Dev

两个稀疏图的交点

来自分类Dev

用一个填充Scipy稀疏矩阵

Related 相关文章

热门标签

归档