Generating a dense matrix from a sparse matrix in numpy python

前端 未结 2 968
自闭症患者
自闭症患者 2020-12-23 14:12

I have a Sqlite database that contains following type of schema:

termcount(doc_num, term , count)

This table contains terms with their resp

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-23 14:19

    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
    

提交回复
热议问题