Python LDA 主题模型:打印每个主题的前 N 个词
这是一个 Python 函数,用于打印出 Latent Dirichlet Allocation (LDA) 模型中每个主题的前 N 个最重要的词,并将这些单词组成一个列表返回。LDA 是一种文本主题建模方法,用于从大量文本数据中自动识别隐藏的主题,并将文档分配给这些主题。该函数使用 sklearn 库实现。
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer
from sklearn.decomposition import LatentDirichletAllocation
def print_top_words(model, feature_names, n_top_words):
tword = []
for topic_idx, topic in enumerate(model.components_):
print('Topic #%d:' % topic_idx)
topic_w = ' '.join([feature_names[i] for i in topic.argsort()[:-n_top_words - 1:-1]])
tword.append(topic_w)
print(topic_w)
return tword
原文地址: https://www.cveoy.top/t/topic/mY9H 著作权归作者所有。请勿转载和采集!