Reputation: 91
According to the documentation of scipy.sparse.csr_matrix
, there are many ways to create a csr_matrix
. One of the ways is to give data
, indptr
, and indices
as inputs. I wonder, if there is a way to retrieve them in the opposite direction, i.e., assume that I have my_csr_matrix
created as below:
>>> indptr = np.array([0, 2, 3, 6])
>>> indices = np.array([0, 2, 2, 0, 1, 2])
>>> data = np.array([1, 2, 3, 4, 5, 6])
>>> my_csr_matrix = csr_matrix((data, indices, indptr), shape=(3, 3))
>>> my_csr_matrix.toarray()
array([[1, 0, 2],
[0, 0, 3],
[4, 5, 6]])
The question is, how can I retrieve indptr and indices from my_csr_matrix
without knowing the information about them or data
before-hand explicitly?
Upvotes: 0
Views: 1202
Reputation: 281968
Literally just my_csr_matrix.indptr
and my_csr_matrix.indices
. You can also get the data array with my_csr_matrix.data
.
These attributes are documented further down the page, under the "Attributes" heading.
Note that these are the actual underlying arrays used by the sparse matrix representation. Modifying these arrays will modify the sparse matrix, unless you do something that causes the CSR matrix to allocate new underlying arrays. (For example, my_csr_matrix[1, 1] = 7
would change the sparsity structure of the matrix and require allocating new arrays.)
Upvotes: 1