访问术语主题矩阵由Gensim LDA生成

问题描述 投票:0回答:1

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

很容易应用转换来检索Document-topic矩阵,如下所示:

doc_topic_matrix = lda_model[doc_term_matrix]

所以我希望有一个类似的功能方法来生成主题 - 术语矩阵。

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

         word1  word2  word3  word4  word5
topic_a   .12    .38    .07    .24    .19
topic_b   .41    .11    .04    .14    .30

有关这是否可能的任何想法?

python gensim lda
1个回答
0
投票

这很简单,你可以这样得到它:

#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)
© www.soinside.com 2019 - 2024. All rights reserved.