gpt4 book ai didi

scikit-learn - 使用 NMF 的主题概率分布

转载 作者:行者123 更新时间:2023-12-04 17:48:53 24 4
gpt4 key购买 nike

我使用以下代码对我的文档进行主题建模:

from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer
tfidf_vectorizer = TfidfVectorizer(tokenizer=tokenize, max_df=0.85, min_df=3, ngram_range=(1,5))

tfidf = tfidf_vectorizer.fit_transform(docs)
tfidf_feature_names = tfidf_vectorizer.get_feature_names()


from sklearn.decomposition import NMF

no_topics = 50

%time nmf = NMF(n_components=no_topics, random_state=11, init='nndsvd').fit(tfidf)
topic_pr= nmf.transform(tfidf)

我认为 topic_pr 给了我每个文档不同主题的概率分布。换句话说,我希望输出(topic_pr)中的数字是第 X 行中的文档属于模型中 50 个主题中的每一个的概率。但是,这些数字不会加到 1。这些真的是概率吗?如果不是,有没有办法将它们转换为概率?

谢谢

最佳答案

NMF 返回非负因式分解,与概率无关(据我所知)。如果你只想要概率,你可以转换 NMF 的输出(L1 归一化)

probs = topic_pr / topic_pr.sum(axis=1, keepdims=True)

这假设 topic_pr是一个非负矩阵,这在你的情况下是正确的。

编辑:显然有一个 NMF 的概率版本。

报价 sklearn's documetation :

Non-negative Matrix Factorization is applied with two different objective functions: the Frobenius norm, and the generalized Kullback-Leibler divergence. The latter is equivalent to Probabilistic Latent Semantic Indexing.



要从同一个链接应用后者,这正是您似乎需要的:
lda = LatentDirichletAllocation(n_components=no_topics, max_iter=5)
topic_pr = lda.fit_transform(tfidf)

关于scikit-learn - 使用 NMF 的主题概率分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46660656/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com