[英]How to get the tf-idf values in gensim in python
我正在使用genism计算我的tf-idf值,如下所示。
texts = [['human', 'interface', 'computer'],
['survey', 'user', 'computer', 'system', 'response', 'time'],
['eps', 'user', 'interface', 'system'],
['system', 'human', 'system', 'eps'],
['user', 'response', 'time'],
['trees'],
['graph', 'trees'],
['graph', 'minors', 'trees'],
['graph', 'minors', 'survey']]
dictionary = corpora.Dictionary(texts)
corpus = [dictionary.doc2bow(text) for text in texts]
tfidf = models.TfidfModel(corpus)
现在,我想获得tf-idf值最高的3个单词。 请帮我!
经过一番搜索后,您似乎可能需要这样做-虽然不是最易读,但可能会起作用。
top_3 = [t[0] for t in
sorted([(word, i, j) for j, text in enumerate(texts) for i, word in enumerate(text)],
key=lambda t: tfidf[t[2]][t[1]])[:3]]
我从文本中选取(word, i, j)
并使用形式为(word, i, j)
的元组跟踪它们的行(如i)和列(如(word, i, j)
。 然后,我根据单词在tfidf
的值对单词进行排序。 然后,我取前3位(使用[:3]
),并用t[0] for t in ...
将该单词从元组中取出,表示t[0] for t in ...
。
可以轻松地对其进行修改以按顺序存储任意数量的单词。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.