python - 使用 bool 掩码对 scipy 稀疏矩阵进行切片

标签 python scipy slice sparse-matrix

我遇到了在 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

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

[[1 0]
 [0 1]
 [1 0]]

在 0.10.1 和 0.12.0 中,我得到

[[0 0 1]
 [1 1 0]
 [0 0 1]]

我不确定这是错误还是我做错了什么。我使用 coo_matrixcsc_matrix 得到相同的结果。

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

最佳答案

在这些情况下,desired_cols 是什么。在最近的 scipy (0.13.0) 中,结果与你的第一个 (0.10.0) 相匹配。如果你想追踪版本中这么远的变化,你可能需要深入研究 scipy 的 github 源代码。

关于python - 使用 bool 掩码对 scipy 稀疏矩阵进行切片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20080332/

相关文章:

python - 切片系列面板

python - pytest - 是否可以在所有测试脚本之间运行脚本/命令?

multithreading - 二维 slice 的线程安全性,其中每个线程在第一维中都有自己的索引

python - 来自 numpy 操作的 numpy.memmap

python - 找出在 matplotlib 中绘制的两条曲线之间的区域(fill_between area)

python - Numpy 中形状的 (4,) 和 (4,1) 有什么区别?

python - scipy.interpolate.make_interp_spline如何检索所有系数?

json - 使用 Echo 或 Gin 框架的大型数组的内存消耗

python - 当我考虑到索引范围时,总是有这个错误 'IndexError: string index out of range'

python - 在 python 中编写一个快速解析器