使用布尔掩码对 scipy 稀疏矩阵进行切片

mba*_*rov 5 python scipy sparse-matrix slice

我在 0.10.0 和 0.10.1 中遇到了切片 scipy 稀疏矩阵如何工作的差异。考虑以下代码:

from numpy import array, ravel
from scipy.sparse import csr_matrix

mat = csr_matrix(array([[1, 0, 0], [0,1,0], [1,0,0]]))
desired_cols = ravel(mat.sum(0)) > 0

print mat[:, desired_cols].A
Run Code Online (Sandbox Code Playgroud)

在 scipy 0.10.0 中,我得到了我期望得到的:

[[1 0]
 [0 1]
 [1 0]]
Run Code Online (Sandbox Code Playgroud)

在 0.10.1 和 0.12.0 中,我得到

[[0 0 1]
 [1 1 0]
 [0 0 1]]
Run Code Online (Sandbox Code Playgroud)

我不确定这是一个错误还是我做错了什么。我使用coo_matrix和得到相同的结果csc_matrix

我试图从矩阵中删除总和为 0 的所有行。我知道这csr_matrix不支持高效的列切片,我不应该这样。

hpa*_*ulj 2

在这些情况下,desired_cols 是什么。在最近的 scipy (0.13.0) 中,结果与您的第一个 (0.10.0) 相匹配。如果您想追踪版本中这么早的更改,您可能必须深入研究 scipy 的 github 源代码。