使用布尔蒙版切片稀疏矩阵 [英] Slicing a scipy sparse matrix using a boolean mask

查看:128
本文介绍了使用布尔蒙版切片稀疏矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在稀疏矩阵稀疏矩阵的工作方式(0.10.0和0.10.1)中,我遇到了不同.考虑以下代码:

I have encountered a difference in how slicing a scipy sparse matrix works in 0.10.0 and 0.10.1. Consider the following piece of code:

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中,我得到了我期望得到的:

In scipy 0.10.0, I get what I expect to get:

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

在0.10.1和0.12.0中,我得到

In 0.10.1 and 0.12.0, I get

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

我不确定这是否是错误或我做错了什么.使用coo_matrixcsc_matrix可以获得相同的结果.

I am not sure if this is a bug or I am doing something wrong. I get the same results using coo_matrix and csc_matrix.

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

I am trying to remove all rows that sum to 0 from the matrix. I understand that csr_matrix does not support efficient column slicing and I should not being this.

推荐答案

在这些情况下,desired_cols是什么.在最新版本(0.13.0)中,结果与您的第一个(0.10.0)相匹配.如果您想追溯版本中的更改,则可能需要深入研究github源代码.

What is desired_cols in these cases. In recent scipy (0.13.0) the results match your first one (0.10.0). You may have to dig into the github source for scipy if you want to track down changes this far back in the versions.

这篇关于使用布尔蒙版切片稀疏矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆