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

标签 python matrix scipy sparse-matrix

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

  • (0, 26) 0.359171459261
  • (0, 25) 0.121145761751
  • (0, 24) 0.316922015914
  • (0, 23) 0.157622038039
  • (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 and (24, 0) 0.316922015914

我想要做的是通过索引删除它们并成为(如果我有 (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.        ]])

转换为coo,并将下三角数据值设置为0

In [880]: Ao = A.tocoo()
In [881]: mask = (Ao.row>Ao.col)
In [882]: mask
Out[882]: 
array([False, False, False, False,  True,  True,  True, False, False,
        True,  True], dtype=bool)
In [883]: Ao.data[mask]=0

转换回 0,并使用 eliminate_zeros 修剪矩阵。

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.        ]])

coocsr 格式都有一个就地 eliminate_zeros 方法。

<小时/>
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]

您可以将此代码作为仅消除 lower_triangle 值的模型,而不是使用 Ao.data[mask]=0

关于Python稀疏矩阵删除除一个之外的重复索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43146968/

相关文章:

python - 无法创建 Python 循环

java - 使用 ThreadPool 并行化矩阵乘法

Numpy 和 Biopython 必须集成吗?

python 包导入模块使用 __init__.py

python - 如何在 Python 中应用 Box-Cox 变换?

c++ - 解释 Python 扩展多线程

python - 按列而不是行键控

3d 到 2d 投影矩阵

scipy - TensorFlow 乔尔斯基分解

python - 与 R 代码相比,需要正确使用 scipy.optimize.fmin_bfgs