2016-06-14 65 views
0

我一直對潛在語義分析(LSA)和應用這個例子:https://radimrehurek.com/gensim/tut2.html如何羣集下使用潛在語義分析(LSA)主題文件

它包括以下主題的條款集羣,但無法找到任何我們可以如何在主題下聚集文件。

在這個例子中,它說'根據LSI看來,「樹」,「圖」和「未成年人」都是相關詞(對第一個主題的方向貢獻最大),而第二個話題實際上與所有其他詞語有關。正如預期的那樣,前五個文檔與第二個主題更爲緊密相關,而其餘四個文檔則與第一個主題相關。

我們如何將這五個文檔與Python代碼關聯到相關主題?

你可以在下面找到我的python代碼。我將不勝感激任何幫助。

from numpy import asarray 
from gensim import corpora, models, similarities 

#https://radimrehurek.com/gensim/tut2.html 
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"] 

# remove common words and tokenize 
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] 

# remove words that appear only once 
all_tokens = sum(texts, []) 
tokens_once = set(word for word in set(all_tokens) if all_tokens.count(word) == 1) 

texts = [[word for word in text if word not in tokens_once] for text in texts] 

dictionary = corpora.Dictionary(texts) 
corp = [dictionary.doc2bow(text) for text in texts] 

tfidf = models.TfidfModel(corp) # step 1 -- initialize a model 
corpus_tfidf = tfidf[corp] 

# extract 400 LSI topics; use the default one-pass algorithm 
lsi = models.lsimodel.LsiModel(corpus=corp, id2word=dictionary, num_topics=2) 

corpus_lsi = lsi[corpus_tfidf] 


#for i in range(0, lsi.num_topics-1): 
for i in range(0, 3): 
    print lsi.print_topics(i) 

for doc in corpus_lsi: # both bow->tfidf and tfidf->lsi transformations are actually executed here, on the fly 
    print(doc) 

回答

1

corpus_lsi有一個9個向量的列表,它是文檔的數量。 每個向量在其第i個索引中存儲此文檔屬於主題i的可能性。 如果您只想將文檔分配給1個主題,請選擇矢量中具有最高值的主題索引。