Gensim LDA生成的访问项主题矩阵

2024-03-29 14:52:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我用gensim训练了一个LDA模型。我的印象是,Lda将数据简化为两个低级矩阵(ref:https://www.analyticsvidhya.com/blog/2016/08/beginners-guide-to-topic-modeling-in-python/),但我似乎不知道如何访问术语主题矩阵。在gensim的文档中,我能找到的唯一参考是.get_topics()属性,但是它提供的格式对我来说没有意义。在

应用转换来检索文档主题矩阵非常简单,如下所示:

doc_topic_matrix = lda_model[doc_term_matrix]

所以我希望有一个类似的函数方法来生成主题项矩阵。在

理想情况下,输出应如下所示:

^{pr2}$

对这是否可能有什么想法?在


Tags: 数据文档https模型ref主题topicdoc
1条回答
网友
1楼 · 发布于 2024-03-29 14:52:31

很简单,你可以这样做:

#get raw topic > word estimates
topics_terms = model.state.get_lambda() 

#convert estimates to probability (sum equals to 1 per topic)
topics_terms_proba = np.apply_along_axis(lambda x: x/x.sum(),1,topics_terms)

# find the right word based on column index
words = [model.id2word[i] for i in range(topics_terms_proba.shape[1])]

#put everything together
pd.DataFrame(topics_terms_proba,columns=words)

相关问题 更多 >