用sklearn-LDA做主题分析,可以做出每个topic下面有若干关键词,怎么确定这些词语的权重呢?权重计算出来在哪里显示,可以导出来么?(或者说可以计算出每个关键词出现的概率么?)
获取LDA模型,并获取每个主题下的前n个关键词
import numpy as np
import pandas as pd
from sklearn.decomposition import LatentDirichletAllocation
# 假设已经得到了LDA模型lda_model
# 获取每个主题下的前n个关键词
def get_top_words(model, feature_names, n_top_words):
topic_words = []
for topic_idx, topic in enumerate(model.components_):
top_features_ind = topic.argsort()[:-n_top_words - 1:-1]
topic_words.append([feature_names[i] for i in top_features_ind])
return topic_words
# 获取每个主题下的前10个关键词
n_top_words = 10
feature_names = np.array(vectorizer.get_feature_names())
topic_words = get_top_words(lda_model, feature_names, n_top_words)
获取每个topic下每个词的权重
# 获取每个主题下每个词的权重
def get_topic_word_weights(model, feature_names):
topic_word_weights = []
for topic_idx, topic in enumerate(model.components_):
word_weights = []
for word_idx, weight in enumerate(topic):
word = feature_names[word_idx]
word_weights.append((word, weight))
topic_word_weights.append(word_weights)
return topic_word_weights
topic_word_weights = get_topic_word_weights(lda_model, feature_names)
以上两种方法都可以得到每个topic下面的关键词,第一种方法可以得到每个关键词的排名,第二种方法可以得到每个关键词的权重。根据需求可以选择不同的方法。
From:肩匣与橘&GPT