Slicing a lean sparse matrix using a boolean mask

I came across a difference in how slicing a scipy sparse matrix works in 0.10.0 and 0.10.1. Consider the following code snippet:

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

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

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

At 0.10.1 and 0.12.0 I get

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

I'm not sure if this is a mistake, or am I doing something wrong. I get the same results using coo_matrixand csc_matrix.

I am trying to remove all rows whose sum is 0 from the matrix. I understand that it csr_matrixdoes not support efficient column separation, and I should not be that.

+4
source share
2 answers

. scipy (0.13.0) (0.10.0). , , github scipy, .

+1

np.flatnonzero(desired_cols) desired_cols scipy.sparse . API scipy.sparse, .

+1

All Articles