gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:34:47 28 4
gpt4 key购买 nike

我有一个文档术语矩阵,是用语料库中术语的共现构建的,如所解释的 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com