python - Python 中的共现矩阵,scipy coo_matrix

标签 python scipy sparse-matrix

我有一个文档术语矩阵,是用语料库中术语的共现构建的,如所解释的 here :

vocabulary = {}  # map terms to column indices
data = []        # values (maybe weights)
row = []         # row (document) indices
col = []         # column (term) indices

import scipy
for i, doc in enumerate(bloblist):
for term in doc:
    # get column index, adding the term to the vocabulary if needed
    j = vocabulary.setdefault(term, len(vocabulary))
    data.append(1)  # uniform weights
    row.append(i)
    col.append(j)
A = scipy.sparse.coo_matrix((data, (row, col)))

>>>print A

(0, 0)  1
(0, 1)  1
(0, 2)  1
(0, 3)  1
...

现在我想将其导出到 csv 或将其写入数据库。我不知道该怎么做,我不知道如何处理稀疏矩阵。

当我尝试时,我总是收到此错误:

TypeError: 'coo_matrix' object has no attribute '__getitem__'

最佳答案

请查看input/output section of scipy.您可以使用 mmwrite 使用 matrix market format 写入矩阵这是稀疏矩阵存储的标准格式。

下面的示例创建随机稀疏矩阵并将其写为 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

关于python - Python 中的共现矩阵,scipy coo_matrix,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31916295/

相关文章:

python - Python 中的大型矩阵乘法 - 什么是最佳选择?

tensorflow - 计算编辑距离(feed_dict 错误)

python - 如何获得两个字典共有的值,即使键不同?

python - 索引错误 : index 2 is out of bounds for axis 0 with size 2

python - 在 Python 中计算加权成对距离矩阵

c++ - Eigen 稀疏 vector :求最大系数

python - 区分同名的 Python 模块/安装不同的名称?

python - 在 Windows 上访问 OpenAI 的 CLI(通过 Jupyter Notebook 文档)

python - 如何打印 SQLAlchemy ORM 中的所有列

python - 计算wav文件FFT时遇到的问题