Horje
how to convert a dense matrix into sparse matrix in python Code Example
how to convert a dense matrix into sparse matrix in python
# dense to sparse
from numpy import array
from scipy.sparse import csr_matrix
# create dense matrix
A = array([[1, 0, 0, 1, 0, 0], [0, 0, 2, 0, 0, 1], [0, 0, 0, 2, 0, 0]])
print(A)
# convert to sparse matrix (CSR method)
S = csr_matrix(A)
print(S)
# reconstruct dense matrix
B = S.todense()
print(B)




Python

Related
python element wise multiplication list Code Example python element wise multiplication list Code Example
boto3 with aws profile Code Example boto3 with aws profile Code Example
python create file Code Example python create file Code Example
datetimes to day of year python Code Example datetimes to day of year python Code Example
python eval set variable Code Example python eval set variable Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
7