将python稀疏矩阵dict转换为scipy稀疏矩阵

che*_*ent 5 python sparse-matrix scikit-learn

我正在使用python scikit-learn进行文档聚类,并且我有一个存储在dict对象中的稀疏矩阵:

例如:

doc_term_dict = { ('d1','t1'): 12,             \
                  ('d2','t3'): 10,             \
                  ('d3','t2'):  5              \
                  }                            # from mysql data table 
<type 'dict'>
Run Code Online (Sandbox Code Playgroud)

我想用来scikit-learn做输入矩阵类型的聚类scipy.sparse.csr.csr_matrix

例:

(0, 2164)   0.245793088885
(0, 2076)   0.205702177467
(0, 2037)   0.193810934784
(0, 2005)   0.14547028437
(0, 1953)   0.153720023365
...
<class 'scipy.sparse.csr.csr_matrix'>
Run Code Online (Sandbox Code Playgroud)

我无法找到转换dict为此csr矩阵的方法(我从未使用过scipy.)

Una*_*dra 5

非常直截了当.首先读取字典并将键转换为适当的行和列.Scipy支持(并为此目的推荐)稀疏矩阵的COO-rdinate格式.

传递它data,rowcolumn,其中A[row[k], column[k] = data[k](对于所有k)定义矩阵.然后让Scipy转换为CSR.

请检查,我有你想要的行和列,我可能会将它们转换.我还假设输入是1索引的.

我的代码打印:

(0, 0)        12
(1, 2)        10
(2, 1)        5
Run Code Online (Sandbox Code Playgroud)

码:

#!/usr/bin/env python3
#http://stackoverflow.com/questions/26335059/converting-python-sparse-matrix-dict-to-scipy-sparse-matrix

from scipy.sparse import csr_matrix, coo_matrix

def convert(term_dict):
    ''' Convert a dictionary with elements of form ('d1', 't1'): 12 to a CSR type matrix.
    The element ('d1', 't1'): 12 becomes entry (0, 0) = 12.
    * Conversion from 1-indexed to 0-indexed.
    * d is row
    * t is column.
    '''
    # Create the appropriate format for the COO format.
    data = []
    row = []
    col = []
    for k, v in term_dict.items():
        r = int(k[0][1:])
        c = int(k[1][1:])
        data.append(v)
        row.append(r-1)
        col.append(c-1)
    # Create the COO-matrix
    coo = coo_matrix((data,(row,col)))
    # Let Scipy convert COO to CSR format and return
    return csr_matrix(coo)

if __name__=='__main__':
    doc_term_dict = { ('d1','t1'): 12,             \
                ('d2','t3'): 10,             \
                ('d3','t2'):  5              \
                }   
    print(convert(doc_term_dict))
Run Code Online (Sandbox Code Playgroud)