以下代码中的lda.show_topics
模块仅打印每个主题的前10个单词的分布,如何打印语料库中所有单词的完整分布?
from gensim import corpora, models
documents = ["Human machine interface for lab abc computer applications",
"A survey of user opinion of computer system response time",
"The EPS user interface management system",
"System and human system engineering testing of EPS",
"Relation of user perceived response time to error measurement",
"The generation of random binary unordered trees",
"The intersection graph of paths in trees",
"Graph minors IV Widths of trees and well quasi ordering",
"Graph minors A survey"]
stoplist = set('for a of the and to in'.split())
texts = [[word for word in document.lower().split() if word not in stoplist]
for document in documents]
dictionary = corpora.Dictionary(texts)
corpus = [dictionary.doc2bow(text) for text in texts]
lda = models.ldamodel.LdaModel(corpus_tfidf, id2word=dictionary, num_topics=2)
for i in lda.show_topics():
print i
答案 0 :(得分:8)
topn
中有一个变量调用show_topics()
,您可以在其中指定每个主题上的字词分布所需的前N个单词的数量。见http://radimrehurek.com/gensim/models/ldamodel.html
因此,而不是默认的lda.show_topics()
。您可以使用len(dictionary)
作为每个主题的完整字词分发:
for i in lda.show_topics(topn=len(dictionary)):
print i
答案 1 :(得分:4)
在num_topics
中有两个变量调用num_words
和show_topics()
,对于num_topics
个主题,返回num_words
个最重要的单词(每个主题10个字) , 默认情况下)。见http://radimrehurek.com/gensim/models/ldamodel.html#gensim.models.ldamodel.LdaModel.show_topics
因此,您可以将len(lda.id2word)
用于每个主题的完整字词分发,并将lda.num_topics
用于lda模型中的所有主题。
for i in lda.show_topics(formatted=False,num_topics=lda.num_topics,num_words=len(lda.id2word)):
print i
答案 2 :(得分:0)
以下代码将打印您的文字及其概率。我打印了前10个单词。您可以更改num_words = 10以在每个主题上打印更多单词。
for words in lda.show_topics(formatted=False,num_words=10):
print(words[0])
print("******************************")
for word_prob in words[1]:
print("(",dictionary[int(word_prob[0])],",",word_prob[1],")",end = "")
print("")
print("******************************")