scikit-learn - python 下如何将dict 转成scipy sparse matrix?
PHP中文网
PHP中文网 2017-04-17 13:37:11
[Python讨论组]

我的数据是从数据库里读出来的,已经是稀疏矩阵了-(doc_a,doc_b,count)

如下:

doc_term_dict={('d1','t1'):12, ('d2','t3'):10, ('d3','t2'):5}
<type 'dict'>

我用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'>

求助如何转换呢?我看着半天都是吧普通dict转成scipy sparse matrix的,没找到如何把一个已经是sparse dict转成scipy sparse matrix?

PHP中文网
PHP中文网

认证高级PHP讲师

全部回复(1)
PHPz

我搞懂了正确转换方法,比较简单

1.先将dict转换成COO matrix,再转换成CSR matrix

    A[row[k], column[k] = data[k]]
    # 创建 COO-matrix
    coo = coo_matrix((data,(row,col)))
    # Scipy 转换 COO 到 CSR format
    return csr_matrix(coo)

代码

    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))
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号