2016-10-11 118 views
6

我希望使用LDA將每個文檔分配給一個主題。現在我意識到你得到的是一個來自LDA主題的分佈。然而,正如你從下面的最後一行看到的,我將它分配給最可能的話題。Gensim LDA主題分配

我的問題是這樣的。爲了獲得這些主題,我必須第二次運行lda[corpus]。有沒有其他的內建gensim函數可以直接給我這個主題賦值向量?特別是由於LDA算法已經通過文檔,它可能已經保存了這些主題分配?

# Get the Dictionary and BoW of the corpus after some stemming/ cleansing 
texts = [[stem(word) for word in document.split() if word not in STOPWORDS] for document in cleanDF.text.values] 
dictionary = corpora.Dictionary(texts) 
dictionary.filter_extremes(no_below=5, no_above=0.9) 
corpus = [dictionary.doc2bow(text) for text in texts] 

# The actual LDA component 
lda = models.LdaMulticore(corpus=corpus, id2word=dictionary, num_topics=30, chunksize=10000, passes=10,workers=4) 

# Assign each document to most prevalent document 
lda_topic_assignment = [max(p,key=lambda item: item[1]) for p in lda[corpus]] 

回答

-1
dictionary = corpora.Dictionary(texts) 
corpus = [dictionary.doc2bow(text) for text in texts] 


test =LDA[corpus[0]] 
print(test) 
sorted(test, reverse=True, key=lambda x: x[1]) 

Topics = ['Topic_'+str(sorted(LDA[i], reverse=True, key=lambda x: x[1])[0][0]).zfill(3) for i in corpus] 
+4

請提供一些背景,解釋爲什麼你的解決方案是正確的。這將有助於OP瞭解它。 –