【发布时间】:2020-01-06 05:02:15
【问题描述】:
我正在尝试获取整个语料库中单词的平均 TF-IDF 值。假设我们的语料库中出现了 4 次“stack”这个词(几百个文档)。它在找到的 4 个文档中具有这些值 0.34, 0.45, 0.68, 0.78。因此,整个语料库的平均 TF-IDF 值为0.5625。我怎样才能找到文档中所有单词的这个?
我正在使用 TF-IDF 的 scikit-learn 实现。这是我用来获取每个文档的 TF-IDF 值的代码:
for i in docs_test:
feature_names=cv.get_feature_names()
doc=docs_test[itr]
itr += 1
tf_idf_vector=tfidf_transformer.transform(cv.transform([doc]))
sorted_items=sort_coo(tf_idf_vector.tocoo())
#Extracting the top 81 keywords along with their TF-IDF scores
keywords=extract_topn_from_vector(feature_names,sorted_items,81)
对于每次迭代,这会输出一个包含 81 个单词的字典以及该文档的 TF-IDF 分数:
{'kerry': 0.396, 'paris': 0.278, 'france': 0.252 ......}
由于我只输出前 81 个单词,我知道该文档中的所有单词都不会被覆盖。所以,我想要文档中前 81 个单词的平均 TF-IDF 值(单词会重复)。
编辑:我尝试了@mijjiga 的解决方案。结果如下:
{'the': 0.51203095036175, 'to': 0.36268858983957286, 'of': 0.3200193439760937, 'in': 0.256015475180875, 'he': 0.2133462293173958}
{'the': 0.5076730825668095, 'to': 0.3299875036684262, 'in': 0.3299875036684262, 'and': 0.30460384954008574, 'trump': 0.17768557889838335}
{'the': 0.5257856140532874, 'children': 0.292103118918493, 'to': 0.2336824951347944, 'winton': 0.2336824951347944, 'of': 0.2336824951347944}
{'the': 0.6082672845890075, 'to': 0.3146210092701763, 'trump': 0.2936462753188312, 'that': 0.23911196704533397, 'of': 0.21394228630371986}
{'the': 0.6285692218670833, 'to': 0.3610929572427925, 'of': 0.2139810116994326, 'that': 0.20060719846821806, 'iran': 0.18723338523700353}
{'the': 0.5730922466510651, 'clinton': 0.29578954665861423, 'of': 0.24032900666012408, 'in': 0.2218421599939607, 'that': 0.2218421599939607}
{'the': 0.7509270472649924, 'to': 0.34926839407674065, 'trump': 0.17463419703837033, 'of': 0.17463419703837033, 'delegates': 0.1571707773345333}
{'on': 0.4, 'administration': 0.2, 'through': 0.2, 'the': 0.2, 'tax': 0.2}
{'the': 0.5885277950982455, 'in': 0.3184973949943446, 'of': 0.3046496821685035, 'to': 0.29080196934266245, 'women': 0.2769542565168214}
正如我们所见,“the”这个词有多个值。如果我的问题没有表明这一点,我深表歉意,但我希望每个单词都有一个值。这个值是该词在该文档语料库中的平均 TF-IDF 分数。关于如何让它工作的任何帮助?谢谢!
这里是使用的代码:
from sklearn.feature_extraction.text import TfidfVectorizer
import numpy as np
itr = 0
for i in range(1,10):
docs=docs_test[itr]
docs=[docs]
itr+=1
tfidf_vectorizer=TfidfVectorizer(use_idf=True)
tfidf_vectorizer_vectors=tfidf_vectorizer.fit_transform(docs)
tfidf = tfidf_vectorizer_vectors.todense()
# TFIDF of words not in the doc will be 0, so replace them with nan
tfidf[tfidf == 0] = np.nan
# Use nanmean of numpy which will ignore nan while calculating the mean
means = np.nanmean(tfidf, axis=0)
# convert it into a dictionary for later lookup
means = dict(zip(tfidf_vectorizer.get_feature_names(), means.tolist()[0]))
tfidf = tfidf_vectorizer_vectors.todense()
# Argsort the full TFIDF dense vector
ordered = np.argsort(tfidf*-1)
words = tfidf_vectorizer.get_feature_names()
top_k = 5
for i, doc in enumerate(docs):
result = { }
# Pick top_k from each argsorted matrix for each doc
for t in range(top_k):
# Pick the top k word, find its average tfidf from the
# precomputed dictionary using nanmean and save it to later use
result[words[ordered[i,t]]] = means[words[ordered[i,t]]]
print (result )
【问题讨论】:
-
为什么
docs=docs_test[itr]应该没有循环,docs应该包含语料库中的所有文档。类似docs=docs_test -
糟糕!这就是问题所在。谢谢!
标签: python scikit-learn tf-idf tfidfvectorizer