首先,MCVexample 会对这个问题有很大帮助。我只能推测你的行操作。
一个基本问题 - 行的稀疏结构是否不同?以lil 为例。如果 2 行具有相同的 rows 列表,那么您的数学可以直接使用 data 列表。如果rows 不同,那么数学变得更加复杂,因为您必须更改两个列表。
lil 和 csr 都支持按行索引
In [7]: M=sparse.rand(10,10,.3)
In [8]: Mr=M.tocsr()
In [9]: Ml=M.tolil()
是的,csr 会在您通过添加另一行来更改行时发出警告:
In [17]: Ml[2,:] += Ml[1,:]
In [18]: Mr[2,:] += Mr[1,:]
...
SparseEfficiencyWarning)
但lil 数学实际上使用了csr 中介。 lil 行表示为列表,而不是数组。
In [14]: Ml[1,:]+Ml[2,:]
Out[14]:
<1x10 sparse matrix of type '<class 'numpy.float64'>'
with 5 stored elements in Compressed Sparse Row format>
索引矩阵运算很慢,尤其是与密集数组等效项相比。但他们会为您处理很多小细节。
我在其他 SO 答案中探索了行操作。当我更好地了解您要做什么时,我会搜索这些内容。
总的来说,没有灵丹妙药,尤其是在您改变稀疏性的情况下。 scipy sparse 不是快速行计算的最佳工具。
scipy: Adding a sparse vector to a specific row of a sparse matrix - 这个问题足够接近,我很想将这个问题标记为重复。
Extremely slow sum row operation in Sparse LIL matrix in Python
(更多内容请参见'user:901925 [scipy] rows' 上的 SO 搜索)