csr_matrix與ndarray類型互轉


ndarry 轉 csr_matrix
>>> import numpy as np
>>> import scipy.sparse

>>> my_matrix = scipy.sparse.csr_matrix((2,2))
>>> my_array = my_matrix.A
>>> type(my_array)
numpy.ndarray
1
2
3
4
5
6
7
csr_matrix 轉 ndarray
>>> import numpy as np
>>> from scipy import sparse
>>> A = np.array([[1,2,0],[0,0,3],[1,0,4]])

>>> A
array([[1, 2, 0],
[0, 0, 3],
[1, 0, 4]])

>>> sA = sparse.csr_matrix(A) # Here's the initialization of the sparse matrix.

>>> sA
<3x3 sparse matrix of type '<type 'numpy.int32'>'
with 5 stored elements in Compressed Sparse Row format>

>>> print sA
(0, 0) 1
(0, 1) 2
(1, 2) 3
(2, 0) 1
(2, 2) 4


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM