Python 自适应稀疏矩阵时的意外行为

Python 自适应稀疏矩阵时的意外行为,python,scipy,sparse-matrix,Python,Scipy,Sparse Matrix,我有一个稀疏矩阵M和一个数组a,其中我想将M增加1。此数组a可能包含重复项,每当元素在a中出现n次时,我想将n添加到M中的相应位置。我是这样做的: from scipy import sparse as sp M = sp.csr_matrix((3, 4), dtype=float) M[[0,0,0,0,0], [0,1,0,1,0]] += 1 但是当我运行这个时,M[0,0]只增加了一个,有没有一个简单的方法来适应它?MATLAB是如何处理的 numpy有一个特殊的函数来处理这个重复的

我有一个稀疏矩阵
M
和一个数组
a
,其中我想将
M
增加1。此数组
a
可能包含重复项,每当元素在
a
中出现
n次时,我想将
n
添加到
M
中的相应位置。我是这样做的:

from scipy import sparse as sp
M = sp.csr_matrix((3, 4), dtype=float)
M[[0,0,0,0,0], [0,1,0,1,0]] += 1

但是当我运行这个时,
M[0,0]
只增加了一个,有没有一个简单的方法来适应它?

MATLAB是如何处理的

numpy
有一个特殊的函数来处理这个重复的索引情况,
add.at

这还没有在
scipy.sparse
中实现

由于当从
coo
格式转换为
csr
格式时,
sparse
对重复的坐标求和,我怀疑这个问题可能是以一种利用这种方式产生的。实际上,
csr
矩阵有一个
M.sum\u duplicates
方法

我得想办法弄清楚细节



添加到
M
会产生相同的缓冲操作-带有警告。改变矩阵的稀疏性是相对昂贵的

In [886]: M[[0,0,0,0,0],[0,1,0,1,0]] += 1
....
  SparseEfficiencyWarning)
In [887]: M
Out[887]: 
<3x4 sparse matrix of type '<class 'numpy.float64'>'
    with 2 stored elements in Compressed Sparse Row format>
In [888]: M.A
Out[888]: 
array([[ 1.,  1.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.]])
现在我们可以添加原始的和新的:

In [898]: M = sparse.csr_matrix((3, 4), dtype=float)
In [899]: M+m
Out[899]: 
<3x4 sparse matrix of type '<class 'numpy.float64'>'
    with 2 stored elements in Compressed Sparse Row format>
[898]中的
:M=sparse.csr_矩阵((3,4),dtype=float)
In[899]:M+M
出[899]:

请考虑阅读NoMPY和ScPy的文档,了解这里发生了什么。因此,基本的计算管道(对于矢量化方法)可能是:a:对位置进行排序(lex),B:创建一个1d的位置向量,在a中合并重复项,同时并行求和B(B的维数可能降低;某些条目可能从1增加到N),C:在使用a的索引时添加这些B值。更简单的方法(基于循环的方法):只需抓住循环中的每个位置并(一个接一个)递增即可。好的,谢谢。我就是这样做的,但我希望会有一个更快的方法。我来自MATLAB,所以我总是希望矩阵运算比循环更快。大多数情况下都是这样。然后试一下我的第一种方法(或者等待一些专家提出更好的方法)。
In [886]: M[[0,0,0,0,0],[0,1,0,1,0]] += 1
....
  SparseEfficiencyWarning)
In [887]: M
Out[887]: 
<3x4 sparse matrix of type '<class 'numpy.float64'>'
    with 2 stored elements in Compressed Sparse Row format>
In [888]: M.A
Out[888]: 
array([[ 1.,  1.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.]])
In [895]: m = sparse.csr_matrix((np.ones(5,int),([0,0,0,0,0],[0,1,0,1,0])), shape=M.shape)
In [896]: m
Out[896]: 
<3x4 sparse matrix of type '<class 'numpy.int32'>'
    with 2 stored elements in Compressed Sparse Row format>
In [897]: m.A
Out[897]: 
array([[3, 2, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]], dtype=int32)
In [898]: M = sparse.csr_matrix((3, 4), dtype=float)
In [899]: M+m
Out[899]: 
<3x4 sparse matrix of type '<class 'numpy.float64'>'
    with 2 stored elements in Compressed Sparse Row format>