对 scipy.sparse.csr_matrix 中的行求和

Sum over rows in scipy.sparse.csr_matrix

我有一个很大的 csr_matrix,我想添加更多的行并获得一个新的 csr_matrix,它具有相同的列数但减少了行数。 (上下文:矩阵是从sklearn CountVectorizer得到的document-term矩阵,我希望能够根据与这些文档相关的代码快速组合文档)

举个最简单的例子,这是我的矩阵:

import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse import vstack

row = np.array([0, 4, 1, 3, 2])
col = np.array([0, 2, 2, 0, 1])
dat = np.array([1, 2, 3, 4, 5])
A = csr_matrix((dat, (row, col)), shape=(5, 5))
print A.toarray()

[[1 0 0 0 0]
 [0 0 3 0 0]
 [0 5 0 0 0]
 [4 0 0 0 0]
 [0 0 2 0 0]]

不,假设我想要一个新矩阵 B,其中行 (1, 4) 和 (2, 3, 5) 通过对它们求和来组合,看起来像这样:

[[5 0 0 0 0]
 [0 5 5 0 0]]

并且应该再次采用稀疏格式(因为我正在处理的真实数据很大)。我尝试对矩阵的切片求和然后堆叠它:

idx1 = [1, 4]
idx2 = [2, 3, 5]
A_sub1 = A[idx1, :].sum(axis=1)
A_sub2 = A[idx2, :].sum(axis=1)
B = vstack((A_sub1, A_sub2))

但这只为切片中的非零列提供了总和值,因此我无法将其与其他切片合并,因为求和切片中的列数不同。

我觉得一定有一种简单的方法可以做到这一点。但我无法在网上或文档中找到对此的任何讨论。我错过了什么?

感谢您的帮助

索引应该是:

idx1 = [0, 3]       # rows 1 and 4
idx2 = [1, 2, 4]    # rows 2,3 and 5

然后你需要保持 A_sub1A_sub2 的稀疏格式并使用 axis=0:

A_sub1 = csr_matrix(A[idx1, :].sum(axis=0))
A_sub2 = csr_matrix(A[idx2, :].sum(axis=0))
B = vstack((A_sub1, A_sub2))
B.toarray()
array([[5, 0, 0, 0, 0],
       [0, 5, 5, 0, 0]])

注意,我认为 A[idx, :].sum(axis=0) 操作涉及从稀疏矩阵转换 - 所以 @Mr_E 的答案可能更好。

或者,当您使用 axis=0np.vstack(而不是 scipy.sparse.vstack)时,它会起作用:

A_sub1 = A[idx1, :].sum(axis=0)
A_sub2 = A[idx2, :].sum(axis=0)
np.vstack((A_sub1, A_sub2))

给予:

matrix([[5, 0, 0, 0, 0],
        [0, 5, 5, 0, 0]])

请注意,您可以通过仔细构造另一个矩阵来做到这一点。以下是它如何适用于密集矩阵:

>>> S = np.array([[1, 0, 0, 1, 0,], [0, 1, 1, 0, 1]])
>>> np.dot(S, A.toarray())
array([[5, 0, 0, 0, 0],
       [0, 5, 5, 0, 0]])
>>>

稀疏版本只是稍微复杂一点。有关应将哪些行加在一起的信息编码在 row:

col = range(5)
row = [0, 1, 1, 0, 1]
dat = [1, 1, 1, 1, 1]
S = csr_matrix((dat, (row, col)), shape=(2, 5))
result = S * A
# check that the result is another sparse matrix
print type(result)
# check that the values are the ones we want
print result.toarray()

输出:

<class 'scipy.sparse.csr.csr_matrix'>
[[5 0 0 0 0]
 [0 5 5 0 0]]

您可以通过在 row 中包含更高的值并相应地扩展 S 的形状来处理输出中的更多行。