小编che*_*ent的帖子

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

我正在使用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.)

python sparse-matrix scikit-learn

5
推荐指数
1
解决办法
3753
查看次数

标签 统计

python ×1

scikit-learn ×1

sparse-matrix ×1