Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract topic word probability matrix in gensim LdaModel

I have the LDA model and the document-topic probabilities.

# build the model on the corpus
ldam = LdaModel(corpus=corpus, num_topics=20, id2word=dictionary) 
# get the document-topic probabilities
theta, _ = ldam.inference(corpus)

I also need the distribution of words for all the topics i.e. a topic-word probability matrix. Is there a way to extract this information?

Thanks!

like image 792
Clock Slave Avatar asked Oct 25 '25 17:10

Clock Slave


1 Answers

The topics-term matrix (lambda) is accessible via :

topics_terms = ldam.state.get_lambda()

If you want a probability distribution just normalize it :

topics_terms_proba = np.apply_along_axis(lambda x: x/x.sum(),1,topics_terms)
like image 110
arthur Avatar answered Oct 28 '25 07:10

arthur