简体   繁体   中英

How can I retrieve indptr, indices from scipy csr_matrix?

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, ie, 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?

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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM