2016-11-01 2 views
2

私は一連の文書を持っており、各文書のトピック分布を知りたい(トピック数の値が異なる)。私はthis questionからおもちゃプログラムを取った。 gensimから提供されたLDAを使用してから、テストデータをトレーニングデータとして与えて、トレーニングデータの各ドキュメントのトピックの分布を取得します。しかし、私はトピックの分布を常に統一しています。ここでgensim LDAモジュール:常に均一な局所分布を得る

は、私がここで

import gensim 
import logging 
logging.basicConfig(filename="logfile",format='%(message)s', level=logging.INFO) 


def get_doc_topics(lda, bow): 
    gamma, _ = lda.inference([bow]) 
    topic_dist = gamma[0]/sum(gamma[0]) # normalize distribution 

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'] 

texts = [[word for word in document.lower().split()] for document in documents] 
dictionary = gensim.corpora.Dictionary(texts) 
id2word = {} 
for word in dictionary.token2id:  
    id2word[dictionary.token2id[word]] = word 
mm = [dictionary.doc2bow(text) for text in texts] 
lda = gensim.models.ldamodel.LdaModel(corpus=mm, id2word=id2word, num_topics=2, update_every=1, chunksize=10000, passes=1,minimum_probability=0.0) 

newdocs=["human system"] 
print lda[dictionary.doc2bow(newdocs)] 

newdocs=["Human machine interface for lab abc computer applications"] #same as 1st doc in training 
print lda[dictionary.doc2bow(newdocs)] 

を使用おもちゃのコードで出力されます:

[(0, 0.5), (1, 0.5)] 
[(0, 0.5), (1, 0.5)] 

私はいくつかのより多くの例で確認しているが、すべてが同じ等確率の結果を与えてしまいました。ここで

は、それが1000年にパスの一切を増やすので、私は試してみました「あまりにもいくつかの更新は、トレーニングが収束していないいない可能性があります」と言う

adding document #0 to Dictionary(0 unique tokens: []) 
built Dictionary(42 unique tokens: [u'and', u'minors', u'generation', u'testing', u'iv']...) from 9 documents (total 69 corpus positions) 
using symmetric alpha at 0.5 
using symmetric eta at 0.5 
using serial LDA version on this node 
running online LDA training, 2 topics, 1 passes over the supplied corpus of 9 documents, updating model once every 9 documents, evaluating perplexity every 9 documents, iterating 50x with a convergence threshold of 0.001000 
too few updates, training might not converge; consider increasing the number of passes or iterations to improve accuracy 
-5.796 per-word bound, 55.6 perplexity estimate based on a held-out corpus of 9 documents with 69 words 
PROGRESS: pass 0, at document #9/9 
topiC#0 (0.500): 0.057*"of" + 0.043*"user" + 0.041*"the" + 0.040*"trees" + 0.039*"interface" + 0.036*"graph" + 0.030*"system" + 0.027*"time" + 0.027*"response" + 0.026*"eps" 
topiC#1 (0.500): 0.088*"of" + 0.061*"system" + 0.043*"survey" + 0.040*"a" + 0.036*"graph" + 0.032*"trees" + 0.032*"and" + 0.032*"minors" + 0.031*"the" + 0.029*"computer" 
topic diff=0.539396, rho=1.000000 

(ロガーのすなわち出力)生成されたログファイルですが、出力は同じです。 (コンバージェンスに関連していませんが、トピックも増えています)

答えて

2

変数newdocsをgensimドキュメントに変換する際に問題があります。 dictionary.doc2bow()は実際にはリストではなく単語のリストを期待しています。 「ヒューマン・システム」を単語「」と解釈するように文書のリストを提供しますが、にはそのような単語はありませんので無視します。

import gensim 
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'] 

texts = [[word for word in document.lower().split()] for document in documents] 
dictionary = gensim.corpora.Dictionary(texts) 

print dictionary.doc2bow("human system".split()) 
print dictionary.doc2bow(["human system"]) 
print dictionary.doc2bow(["human"]) 
print dictionary.doc2bow(["foo"]) 

だから上記のコードを修正するために、次のコードの出力を参照してください私のポイントをより明確にするためにあなたがしなければならないのは仕方によって、以下の

newdocs = "human system".lower().split() 
newdocs = "Human machine interface for lab abc computer applications".lower().split() 

ああに従ってnewdocs変更されますあなたが観察する行動は、同じ確率を得て、単純に空の文書のトピック分布です。

+0

完璧!ありがとうございました !そして、私はもう一つ知る必要があります。これらのすべてをやっている私の主な目的は、質問で述べたように、話題の話題分布を得ることです。私がコードで使った小さなハックを使ってLDAを実行した後に、より良い方法があるのですか?(テストセットとしてのトレーニングを提供しています!) – MysticForce

関連する問題