gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:55:53 25 4
gpt4 key购买 nike

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

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

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

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) 函数计算文档-主题关联。

在示例代码中,这将是:

doc_topic_distrib = lda.transform(tf)

使用 lda 拟合的 lda,以及 tf 要转换的输入数据

关于python - 主题建模 - 将前 2 个主题的文档分配为类别标签 - sklearn Latent Dirichlet Allocation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34429635/

25 4 0