Setup
I'm aware of the fact that sparse matrices in scipy's .sparse
-module differ from numpy
-arrays. Also, I'm aware of questions like here regarding slicing of sparse arrays. Anyhow, this and most other questions deal with the performance of slicing.
My question rather deals with how to cope with their different slicing-behaviour. Lets create an example:
import numpy as np
from scipy import sparse
matrix = np.asarray([[0,0,0,1], [1,1,0,0], [1,0,1,0], [1,0,0,1], [1,0,0,1], [1,0,0,1]])
sparse_matrix = sparse.lil_matrix(matrix) # Or another format like .csr_matrix etc.
Given this setup, applying the same slice results in a different output:
matrix[:, 3]
# Output:
# array([ True, False, False, True, True, True], dtype=bool)
sparse_matrix[:, 3]
# Output:
# matrix([[ True],
# [False],
# [False],
# [ True],
# [ True],
# [ True]], dtype=bool)
Question
This is a bit of a bummer, since I need the first output to apply in the second case as well. As said in the beginning, I know that using sparse_matrix.A
etc. will give me the desired result. Anyhow, converting the sparse matrix to an array contradicts with the initial use-case of sparse-matrices.
So is there some possibility to achieve the same slice-result without converting sparse-matrix
to an array?
Edit:
For clarification, since my question might be confusing regarding this: The slice on the sparse_matrix
shall have the same output as matrix
, meaning that something like sparse_matrix[:, 3]
shall output ([ True, False, False, True, True, True])
.