当我在n_components中指定3个主题时,我只能使我的LDA模型将值返回给一个主题。这是因为它只能找到一个主题吗?
下面是遵循https://ourcodingclub.github.io/2018/12/10/topic-modelling-python.html指令的完整代码:
cvec = CountVectorizer(ngram_range = (2,2), token_pattern='\w+|\$[\d\.]+|\S+')
billie_tf = cvec.fit_transform(X).toarray()
billie_featurenames = cvec.get_feature_names()
lda_model = LatentDirichletAllocation(n_components=3, random_state = 77)
#fit model to lyrics
lda_model.fit(billie_tf)
def display_topics(model, feature_names, no_top_words):
topic_dict ={}
for topic_idx, topic in enumerate(model.components_):
topic_dict['Topic %d words' % (topic_idx)] = ['{}'.format(feature_names[i])
for i in topic.argsort()[:-no_top_words - 1: -1]]
topic_dict['Topic %d weights' % (topic_idx)] = ['{:.1f}'.format(topic[i])
for i in topic.argsort()[:-no_top_words -1 : -1]]
return pd.DataFrame(topic_dict)
display_topics(lda_model, billie_featurenames, 10)