Question: How can I split 1 sparse matrix into 2, based on the values in a list?
That is, I have a sparse matrix X:
>>print type(X)
<class 'scipy.sparse.csr.csr_matrix'>
that I visualize in my head as a list of lists, to look like this:
>>print X.todense()
[[1,3,4]
[3,2,2]
[4,8,1]]
And I have a list y that looks like this:
y = [-1,
3,
-4]
How can I separate X into two sparse matrices, depending on whether the corresponding value in y is positive or negative? For example, how can I get:
>>print X_pos.todense()
[[3,2,2]]
>>print X_neg.todense()
[[1,3,4]
[4,8,1]]
The result (X_pos and X_neg) should also be sparse matrices obviously as it’s just splitting a sparse matrix to begin with.
Thanks!
Use
np.whereto generate two arrays of indices for the positive and negativeyvalues, then use those to index into your sparse matrix.You now have to CSR matrices containing the desired elements: