python - 将文本语料库转换为具有vocabulary_id 和相应 tfidf 分数的文本文档

标签 python machine-learning text-mining tf-idf

我有一个包含 5 个文档的文本语料库,每个文档之间用/n 分隔。我想为文档中的每个单词提供一个 id 并计算其各自的 tfidf 分数。 例如,假设我们有一个名为“corpus.txt”的文本语料库,如下所示:-

“堆栈 溢出 文本向量化 scikit python scipy 稀疏 csr" 使用计算 tfidf 时

mylist =list("corpus.text")
vectorizer= CountVectorizer
x_counts = vectorizer_train.fit_transform(mylist) 
tfidf_transformer = TfidfTransformer()
x_tfidf = tfidf_transformer.fit_transform(x_counts)

输出为

(0,12) 0.1234 #for 1st document
(1,8) 0.3456  #for 2nd  document
(1,4) 0.8976
(2,15) 0.6754 #for third document
(2,14) 0.2389
(2,3) 0.7823
(3,11) 0.9897 #for fourth document
(3,13) 0.8213
(3,5) 0.7722
(3,6) 0.2211
(4,7) 0.1100 # for fifth document
(4,10) 0.6690
(4,2) 0.0912
(4,9) 0.2345
(4,1) 0.1234

我将此scipy.sparse.csr矩阵转换为列表列表以删除文档ID,并仅保留vocabulary_id及其各自的tfidf分数,使用:

m = x_tfidf.tocoo()
mydata = {k: v for k, v in zip(m.col, m.data)} 
key_val_pairs = [str(k) + ":" + str(v) for k, v in mydata.items()] 

但问题是我得到的输出中,vocabulary_id 及其各自的 tfidf 分数按升序排列,并且没有任何对文档的引用。

例如,对于上面给定的语料库,我当前的输出(我已使用 json 转储到文本文件中)如下所示:

1:0.1234
2:0.0912
3:0.7823
4:0.8976
5:0.7722
6:0.2211
7:0.1100
8:0.3456
9:0.2345
10:0.6690
11:0.9897
12:0.1234
13:0.8213
14:0.2389
15:0.6754

而我希望我的文本文件如下所示:

12:0.1234
8:0.3456 4:0.8976
15:0.1234 14:0.2389 3:0.7823
11:0.9897 13:0.8213 5:0.7722 6:0.2211
7:0.1100 10:0.6690 2:0.0912 9:0.2345 1:0.1234

知道如何完成它吗?

最佳答案

我想这就是你所需要的。这里corpus是文档的集合。

from sklearn.feature_extraction.text import TfidfVectorizer
corpus = ["stack over flow stack over flow text vectorization scikit", "stack over flow"]

vectorizer = TfidfVectorizer()
x = vectorizer.fit_transform(corpus) # corpus is a collection of documents

print(vectorizer.vocabulary_) # vocabulary terms and their index
print(x) # tf-idf weights for each terms belong to a particular document

打印:

{'vectorization': 5, 'text': 4, 'over': 1, 'flow': 0, 'stack': 3, 'scikit': 2}
  (0, 2)    0.33195438857 # first document, word = scikit
  (0, 5)    0.33195438857 # word = vectorization
  (0, 4)    0.33195438857 # word = text
  (0, 0)    0.472376562969 # word = flow
  (0, 1)    0.472376562969 # word = over
  (0, 3)    0.472376562969 # word = stack
  (1, 0)    0.57735026919 # second document
  (1, 1)    0.57735026919
  (1, 3)    0.57735026919

根据这些信息,您可以按照您想要的方式表示文档,如下所示:

cx = x.tocoo()
doc_id = -1
for i,j,v in zip(cx.row, cx.col, cx.data):
    if doc_id == -1:
        print(str(j) + ':' + "{:.4f}".format(v), end=' ')
    else:
        if doc_id != i:
            print()
        print(str(j) + ':' + "{:.4f}".format(v), end=' ')
    doc_id = i

打印:

2:0.3320 5:0.3320 4:0.3320 0:0.4724 1:0.4724 3:0.4724 
0:0.5774 1:0.5774 3:0.5774

关于python - 将文本语料库转换为具有vocabulary_id 和相应 tfidf 分数的文本文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40742105/

相关文章:

machine-learning - "Oracle"在机器学习中意味着什么?

r - 如何将从 R 文本挖掘中获得的 termDocumentMatrix 转换为 excel 或 CSV 文件?

r - 使用 tm-package 进行文本挖掘 - 词干提取

Python 发送带有 TITUS 分类的 outlook 邮件

python - 子对象和父对象

c++ - 如何在 Waffles C++ API 中预测实例的类

machine-learning - tensorflow 中的反卷积(conv2d_transpose)

python - 从图像中提取文本

Python 不等式运算符;比较列表

python - Gunicorn/Django,导入错误 : No module named application. wsgi