Python中的共现矩阵,scipy coo_矩阵
我有一个文档术语矩阵,根据语料库中术语的共现情况构建,如下所述: 现在我想将其导出到csv或在db中写入。我不知道怎么做,我不知道如何处理稀疏矩阵 当我尝试时,我总是收到以下错误:Python中的共现矩阵,scipy coo_矩阵,python,scipy,sparse-matrix,Python,Scipy,Sparse Matrix,我有一个文档术语矩阵,根据语料库中术语的共现情况构建,如下所述: 现在我想将其导出到csv或在db中写入。我不知道怎么做,我不知道如何处理稀疏矩阵 当我尝试时,我总是收到以下错误: TypeError: 'coo_matrix' object has no attribute '__getitem__' 请查看,您可以使用mmwrite编写矩阵,使用是稀疏矩阵存储的标准格式 下面是创建随机稀疏矩阵并将其写入MM文件的示例: >>> import scipy.sparse &g
TypeError: 'coo_matrix' object has no attribute '__getitem__'
请查看,您可以使用mmwrite
编写矩阵,使用是稀疏矩阵存储的标准格式
下面是创建随机稀疏矩阵并将其写入MM文件的示例:
>>> import scipy.sparse
>>> A = scipy.sparse.rand(20, 20)
>>> print A
(3, 4) 0.0579085844686
(14, 9) 0.914421740712
(15, 10) 0.622861279405
(5, 17) 0.83146022149
>>> import scipy.io
>>> scipy.io.mmwrite('output', A)
output.mtx的内容:
→ cat output.mtx
%%MatrixMarket matrix coordinate real general
%
20 20 4
4 5 0.05790858446861069
15 10 0.9144217407118101
16 11 0.6228612794046831
6 18 0.8314602214903816
scipy
有许多稀疏矩阵格式。您可以使用允许访问其成员的方法,如到\u csc()
或到\u csr()
将矩阵转换为其他类型之一
→ cat output.mtx
%%MatrixMarket matrix coordinate real general
%
20 20 4
4 5 0.05790858446861069
15 10 0.9144217407118101
16 11 0.6228612794046831
6 18 0.8314602214903816