Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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

PREVIOUS NEXT
Code Example
Python :: django templateview 
Python :: Can only use .str accessor with string values! 
Python :: python scatterplot figsize 
Python :: elbow method k means sklearn 
Python :: how to write in google chrome console in python 
Python :: bring tkinter window to front 
Python :: pandas add a column with loc 
Python :: tf tensor from numpy 
Python :: python create tuple from input 
Python :: how to plotting points on matplotlib 
Python :: how to reverse a number in python 
Python :: streamlit button to load a file 
Python :: tbc full form in cricket 
Python :: how to change angle of 3d plot python 
Python :: how to show multiple image in plt.imshow 
Python :: find matches between two lists python 
Python :: how to get the index of a value in pandas dataframe 
Python :: bulk file name changer in python 
Python :: native bold text 
Python :: tqdm in python 
Python :: is root node an internal node 
Python :: python negative infinity 
Python :: print nested list in new lines in python 
Python :: extract image from pdf python 
Python :: loading text file delimited by tab into pandas 
Python :: how to make nmap port scanner in python 
Python :: django run queryset in terminal 
Python :: python insert image 
Python :: how to save a dictionary as a file in python 
Python :: python selenium screenshot 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =