2013-07-15 22 views
6

lda.show_topics次のコードのモジュールは、トピックごとに上位10語の分布のみを出力しますが、コーパス内のすべての単語の完全分布をどのように出力しますか?gensimのLDAトピックの単語の完全な配布方法は?

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

あなたはそれらのすべてを印刷したり、あなたのプログラムにそれのために彼らのコードをコピーして、それを変更するハックことを行う、とのsite-packages(またはどこそれはあなたのコンピュータ上にある)でLDAパッケージを変更することができます10の代わりにすべてを印刷します。 – debianplebian

+0

は答えを見つけました。それはapi =の中に隠されています)。以下の答えを参照してください。 – alvas

+0

あなた自身の答えを見つけることは良い仕事です。 – debianplebian

答えて

8

あなたは、各トピック語以上の分布から必要な上位N個の単語の数を指定することができますshow_topics()における変数の呼び出しtopnがあります。 http://radimrehurek.com/gensim/models/ldamodel.html

lda.show_topics()の代わりにです。でnum_words最も重要な単語(トピックごとに10個の単語を、話題のnum_topics数のために、2つの変数呼び出しnum_topicsshow_topics()num_wordsあり

for i in lda.show_topics(topn=len(dictionary)): 
    print i 
3

返す:あなたは、各トピックのための完全な単語分布のためlen(dictionary)を使用することができますデフォルト)。 http://radimrehurek.com/gensim/models/ldamodel.html#gensim.models.ldamodel.LdaModel.show_topics

したがって、各トピックの完全な単語の分布にはlen(lda.id2word)、ldaモデルのすべてのトピックにはlda.num_topicsを使用できます。

for i in lda.show_topics(formatted=False,num_topics=lda.num_topics,num_words=len(lda.id2word)): 
    print i 
+0

あなたの答えを説明してください。 SOは単に質問に答えるのではなく、人々が学ぶのを助けるために存在します。コードのみの回答は低品質とみなされます – Machavity

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("******************************") 
関連する問題