Python稀疏矩阵是否删除除一个之外的重复索引?

Python稀疏矩阵是否删除除一个之外的重复索引?,python,matrix,scipy,sparse-matrix,Python,Matrix,Scipy,Sparse Matrix,我在计算向量矩阵之间的余弦相似性,得到的结果是稀疏矩阵,如下所示: (0,26)0.359171459261 (0,25)0.121145761751 (0,24)0.316922015914 (0,23)0.15762038039 (0,22)0.636466644041 (0,21)0.136216495731 (0,20)0.243164535496 (0,19)0.348272617805 (0,18)0.636466644041 (0,17)1.0 但也有重复的情况,例如: (0,

我在计算向量矩阵之间的余弦相似性,得到的结果是稀疏矩阵,如下所示:

  • (0,26)0.359171459261
  • (0,25)0.121145761751
  • (0,24)0.316922015914
  • (0,23)0.15762038039
  • (0,22)0.636466644041
  • (0,21)0.136216495731
  • (0,20)0.243164535496
  • (0,19)0.348272617805
  • (0,18)0.636466644041
  • (0,17)1.0
但也有重复的情况,例如:

(0,24)0.316922015914和(24,0)0.316922015914

我想做的是通过indice和be(如果我有(0,24),那么我不需要(24,0),因为它是相同的)只剩下一个,并删除第二个,对于矩阵中的所有向量。 目前,我有以下代码来创建矩阵:

vectorized_words = sparse.csr_matrix(vectorize_words(nostopwords,glove_dict))
cos_similiarity = cosine_similarity(vectorized_words,dense_output=False)
总之,我不想删除所有的重复项,我想用pythonic的方法只剩下其中一个


提前谢谢你

我认为最容易得到
coo
格式矩阵的上三角:

首先制作一个小的对称矩阵:

In [876]: A = sparse.random(5,5,.3,'csr')
In [877]: A = A+A.T
In [878]: A
Out[878]: 
<5x5 sparse matrix of type '<class 'numpy.float64'>'
    with 11 stored elements in Compressed Sparse Row format>
In [879]: A.A
Out[879]: 
array([[ 0.        ,  0.        ,  0.81388978,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.73944395,  0.20736975,  0.98968617],
       [ 0.81388978,  0.73944395,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.20736975,  0.        ,  0.05581152,  0.04448881],
       [ 0.        ,  0.98968617,  0.        ,  0.04448881,  0.        ]])
转换回0,并使用
消除0
修剪矩阵

In [890]: A1 = Ao.tocsr()
In [891]: A1
Out[891]: 
<5x5 sparse matrix of type '<class 'numpy.float64'>'
    with 11 stored elements in Compressed Sparse Row format>
In [892]: A1.eliminate_zeros()
In [893]: A1
Out[893]: 
<5x5 sparse matrix of type '<class 'numpy.float64'>'
    with 6 stored elements in Compressed Sparse Row format>
In [894]: A1.A
Out[894]: 
array([[ 0.        ,  0.        ,  0.81388978,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.73944395,  0.20736975,  0.98968617],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.05581152,  0.04448881],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ]])

不使用Ao.data[mask]=0您可以将此代码作为仅消除较低三角形值的模型。

将单词矢量化和余弦相似性从何而来?生成
cos\u相似度时删除“重复项”可能比生成后从矩阵中删除“重复项”更容易<代码>稀疏
矩阵不是为单个元素操作而设计的。
scipy.spatial.distance.squareform
转换为消除重复的紧凑上三角形式。我不知道是否有一个版本可以处理稀疏矩阵。@hpaulj cosine\u相似度来自sklearn,矢量化单词是我的功能,让每个单词矢量不会“消除零”删除所有零?我的意思是,我可能在某个地方有一个来自原始矩阵的0值,它也会删除它?是的,它会。我将为
coo
消除零
添加代码,以防您想将其调整为直接使用
掩码
。非常感谢
In [890]: A1 = Ao.tocsr()
In [891]: A1
Out[891]: 
<5x5 sparse matrix of type '<class 'numpy.float64'>'
    with 11 stored elements in Compressed Sparse Row format>
In [892]: A1.eliminate_zeros()
In [893]: A1
Out[893]: 
<5x5 sparse matrix of type '<class 'numpy.float64'>'
    with 6 stored elements in Compressed Sparse Row format>
In [894]: A1.A
Out[894]: 
array([[ 0.        ,  0.        ,  0.81388978,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.73944395,  0.20736975,  0.98968617],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.05581152,  0.04448881],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ]])
def eliminate_zeros(self):
    """Remove zero entries from the matrix

    This is an *in place* operation
    """
    mask = self.data != 0
    self.data = self.data[mask]
    self.row = self.row[mask]
    self.col = self.col[mask]