Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating a dense matrix from a sparse matrix in numpy python

People also ask

How do you make a sparse matrix dense matrix in python?

1 Answer. You can use either todense() or toarray() function to convert a CSR matrix to a dense matrix.

How do you convert a sparse to a dense matrix?

A dense matrix stored in a NumPy array can be converted into a sparse matrix using the CSR representation by calling the csr_matrix() function.

What does Csr_matrix do in Python?

The function csr_matrix() is used to create a sparse matrix of compressed sparse row format whereas csc_matrix() is used to create a sparse matrix of compressed sparse column format.

How do you transpose a sparse matrix in python?

Transposing a sparse matrix is simple enough, we just have to swap the row and column values and then sort the rows in the sparse matrix.


 from scipy.sparse import csr_matrix
 A = csr_matrix([[1,0,2],[0,3,0]])
 >>>A
 <2x3 sparse matrix of type '<type 'numpy.int64'>'
    with 3 stored elements in Compressed Sparse Row format>
 >>> A.todense()
   matrix([[1, 0, 2],
           [0, 3, 0]])
 >>> A.toarray()
      array([[1, 0, 2],
            [0, 3, 0]])

this is an example of how to convert a sparse matrix to a dense matrix taken from scipy


I solved this problem using Pandas. Because we want to keep the document ids and term ids.

from pandas import DataFrame 

# A sparse matrix in dictionary form (can be a SQLite database). Tuples contains doc_id        and term_id. 
doc_term_dict={('d1','t1'):12, ('d2','t3'):10, ('d3','t2'):5}

#extract all unique documents and terms ids and intialize a empty dataframe.
rows = set([d for (d,t) in doc_term_dict.keys()])  
cols = set([t for (d,t) in doc_term_dict.keys()])
df = DataFrame(index = rows, columns = cols )
df = df.fillna(0)

#assign all nonzero values in dataframe
for key, value in doc_term_dict.items():
    df[key[1]][key[0]] = value   

print df

Output:

    t2  t3  t1
d2  0  10   0
d3  5   0   0
d1  0   0  12