通过scikit-learn查找LDA每个主题的文档数量

use*_*014 6 lda scikit-learn

我正在跟随scikit-learn LDA示例,并试图了解我如何(如果可能的话)表明有多少文档被标记为具有这些主题中的每一个.我一直在研读通过文档的LDA模型在这里却看不到哪儿能找到这个号码.有没有人能够在scikit-learn之前做到这一点?

Pat*_*o G 5

LDA计算话题的概率列表每个文档,所以你可能想诠释的话题文档与该文档概率最高的话题。

如果dtm是您的文档术语矩阵和lda潜在狄利克雷分配对象,您可以使用transform()函数和探索主题混合pandas

docsVStopics = lda.transform(dtm)
docsVStopics = pd.DataFrame(docsVStopics, columns=["Topic"+str(i+1) for i in range(N_TOPICS)])
print("Created a (%dx%d) document-topic matrix." % (docsVStopics.shape[0], docsVStopics.shape[1]))
docsVStopics.head()
Run Code Online (Sandbox Code Playgroud)

您可以轻松找到每个文档最可能的主题:

most_likely_topics = docsVStopics.idxmax(axis=1)
Run Code Online (Sandbox Code Playgroud)

然后得到计数:

 most_likely_topics.groupby(most_likely_topics).count()
Run Code Online (Sandbox Code Playgroud)