主题建模 - 将具有前2个主题的文档指定为类别标签 - sklearn Latent Dirichlet Allocation

Bal*_*ala 9 python python-2.7 lda topic-modeling scikit-learn

我现在正在通过LDA(Latent Dirichlet Allocation)主题建模方法来帮助从一组文档中提取主题.从我从下面的链接中理解的,这是一种无监督的学习方法,用提取的主题对每个文档进行分类/标记.

非负矩阵分解和潜在Dirichlet分配的主题提取

在该链接中给出的示例代码中,定义了一个函数来获取与所识别的每个主题相关联的顶部单词.

sklearn.__version__
Run Code Online (Sandbox Code Playgroud)

出[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)
Run Code Online (Sandbox Code Playgroud)

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

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

cle*_*aut 9

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

在示例代码中,这将是:

doc_topic_distrib = lda.transform(tf)
Run Code Online (Sandbox Code Playgroud)

与lda相匹配的lda,以及你想要转换的输入数据

  • 你得到的是每个文档的主题分布。所以每一行对应一个文档,每一列对应一个主题。为了得到你想要的结果,你可以做的是查看每一行并获取三个最大值的列索引。这将为您提供每个文档的三个最重要的主题。 (2认同)