主题建模 - 将前 2 个主题的文档分配为类别标签 - sklearn Latent Dirichlet Allocation

Topic modelling - Assign a document with top 2 topics as category label - sklearn Latent Dirichlet Allocation

我现在正在通过 LDA(Latent Dirichlet Allocation)主题建模方法来帮助从一组文档中提取主题。根据我从下面的 link 中了解到的情况,这是一种无监督学习方法,可以使用提取的主题对每个文档进行分类/标记。

Topic extraction with Non-negative Matrix Factorization and Latent Dirichlet Allocation

在 link 中给出的示例代码中,定义了一个函数来获取与每个已识别主题关联的热门词。

sklearn.__version__

Out[41]: '0.17'

from sklearn.decomposition import LatentDirichletAllocation 


def print_top_words(model, feature_names, n_top_words):
    for topic_idx, topic in enumerate(model.components_):
        print("Topic #%d:" % topic_idx)
        print(" ".join([feature_names[i]
                        for i in topic.argsort()[:-n_top_words - 1:-1]]))
    print()

print("\nTopics in LDA model:")
tf_feature_names = tf_vectorizer.get_feature_names()
print_top_words(lda, tf_feature_names, n_top_words)

我的问题是这样的。是否有构建模型 LDA 的任何组件或矩阵,从那里我们可以获得文档主题关联

例如,我需要找到与每个文档关联的前 2 个主题作为该文档的文档标签/类别。是否有任何组件可以查找文档中的主题分布,类似于 model.components_ 用于查找主题内的单词分布。

您可以使用 LDA 的 transform(X) 函数计算文档-主题关联 class。

在示例代码中,这将是:

doc_topic_distrib = lda.transform(tf)

lda 是拟合的 lda,tf 是要转换的输入数据