我有A
格式的矩阵coo
(由scipy.sparse.bmat
创建):
A
<80000x80000 sparse matrix of type '<class 'numpy.float64'>'
with 278480 stored elements in COOrdinate format>
我想
spsolve(A, g)
的某些g
醇>
现在,我无法更改coo
格式的行。以下作品:
AA = A.tocsr()
AA[10, :] = 1 # or whatever other array I want to put here
spsolve(AA, g)
但我会得到一个
SparseEfficiencyWarning: Changing the sparsity structure of a csr_matrix is expensive. lil_matrix is more efficient.
SparseEfficiencyWarning)
现在,据我所知,lil_matrix
在解决线性代数方面效率很低 - 所以即使我将稀疏性结构更改为lil
,我也应该更改回{{1}之后我或csr
。
答案 0 :(得分:1)
尽管有警告,但csr
的一次性更改优于lil
的往返次数:
In [137]: %%timeit M = sparse.random(10,10,.2,format='csr')
...: M[-1,:] = np.arange(10)
...:
SparseEfficiencyWarning)
204 µs ± 5.06 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
In [139]: %%timeit M = sparse.random(10,10,.2,format='csr')
...: M1=M.tolil(); M1[-1,:] = np.arange(10); M = M1.tocsr()
444 µs ± 9.91 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
In [141]: %%timeit M = sparse.random(10,10,.2,format='lil')
...: M[-1,:] = np.arange(10)
162 µs ± 84.2 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)