2017-04-11 23 views
3

gensim中的ldamodel有兩種方法:get_document_topicsget_term_topicsget_document_topics和get_term_topics在gensim中

儘管他們在這gensim教程notebook使用,我不完全瞭解如何解釋的get_term_topics輸出,創造了自包含下面的代碼來說明我的意思:

from gensim import corpora, models 

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

# build the corpus, dict and train the model 
dictionary = corpora.Dictionary(texts) 
corpus = [dictionary.doc2bow(text) for text in texts] 
model = models.ldamodel.LdaModel(corpus=corpus, id2word=dictionary, num_topics=2, 
           random_state=0, chunksize=2, passes=10) 

# show the topics 
topics = model.show_topics() 
for topic in topics: 
    print topic 
### (0, u'0.159*"system" + 0.137*"user" + 0.102*"response" + 0.102*"time" + 0.099*"eps" + 0.090*"human" + 0.090*"interface" + 0.080*"computer" + 0.052*"survey" + 0.030*"minors"') 
### (1, u'0.267*"graph" + 0.216*"minors" + 0.167*"survey" + 0.163*"trees" + 0.024*"time" + 0.024*"response" + 0.024*"eps" + 0.023*"user" + 0.023*"system" + 0.023*"computer"') 

# get_document_topics for a document with a single token 'user' 
text = ["user"] 
bow = dictionary.doc2bow(text) 
print "get_document_topics", model.get_document_topics(bow) 
### get_document_topics [(0, 0.74568415806946331), (1, 0.25431584193053675)] 

# get_term_topics for the token user 
print "get_term_topics: ", model.get_term_topics("user", minimum_probability=0.000001) 
### get_term_topics: [(0, 0.1124525558321441), (1, 0.006876306738765027)] 

對於get_document_topics中,產出很有意義。兩個概率合計爲1.0,並且user具有較高概率(來自model.show_topics())的主題也具有較高的概率分配。

但對於get_term_topics,有問題:

  1. 的概率加起來還不到1.0,爲什麼呢?
  2. 儘管在數值上,user具有較高概率(來自model.show_topics())的主題也有更高的編號分配,這個數字意味着什麼?
  3. 爲什麼我們應該使用get_term_topics,當get_document_topics可以提供(看似)相同的功能和有意義的輸出?

回答

3

我正在研究LDA主題建模並且遇到了這篇文章。我確實創建了兩個主題,比如說topic1和topic2。

頂部10個字的每個主題如下: 0.009*"would" + 0.008*"experi" + 0.008*"need" + 0.007*"like" + 0.007*"code" + 0.007*"work" + 0.006*"think" + 0.006*"make" + 0.006*"one" + 0.006*"get

0.027*"ierr" + 0.018*"line" + 0.014*"0.0e+00" + 0.010*"error" + 0.009*"defin" + 0.009*"norm" + 0.006*"call" + 0.005*"type" + 0.005*"de" + 0.005*"warn

最後,我把1號文件,用於確定最接近的主題。

for d in doc: 
    bow = dictionary.doc2bow(d.split()) 
    t = lda.get_document_topics(bow) 

並且輸出是[(0, 0.88935698141006414), (1, 0.1106430185899358)]

要回答您的第一個問題,文檔中的概率加起來爲1.0,這就是get_document_topics所做的。該文件明確指出,它返回給定文檔的主題分佈,作爲(topic_id,topic_probability)2元組列表。

此外,我試圖get_term_topics關鍵字「IERR」

t = lda.get_term_topics("ierr", minimum_probability=0.000001),其結果是[(1, 0.027292299843400435)]這不過是確定每個主題,這是有意義的字貢獻。

因此,您可以根據您使用get_document_topics獲得的主題分佈來標記文檔,並且可以根據get_term_topics給出的貢獻來確定該詞的重要性。

我希望這會有所幫助。