【发布时间】:2021-01-03 13:59:20
【问题描述】:
我用sklearn 制作了一个 LDA 模型,但听起来很奇怪,我在网上找不到任何关于如何获得热门词的信息。这是我的代码:
from sklearn.feature_extraction.text import CountVectorizer
count_vect = CountVectorizer()
doc_term_matrix = count_vect.fit_transform(tweet_tp['text'].values.astype('U'))
doc_term_matrix
from sklearn.decomposition import LatentDirichletAllocation
LDA = LatentDirichletAllocation(n_components=3, random_state=1)
id_topic = LDA.fit(doc_term_matrix)
一旦我添加了这个:
import numpy as np
vocab = count_vect.get_feature_names()
topic_words = {}
for topic, comp in enumerate(LDA.components_):
word_idx = np.argsort(comp)[::-1][:5]
topic_words[topic] = [vocab[i] for i in word_idx]
for topic, words in topic_words.items():
print('Topic: %d' % topic)
print(' %s' % ', '.join(words))
我在这里找到了答案,但目前找不到。但是,这只会输出第二个主题词。
【问题讨论】:
标签: python scikit-learn lda topic-modeling