pandas - 为 LDA 主题模型寻找前 3 个主要主题
问题描述
我正在通过这个 LDA 建模教程(https://www.machinelearningplus.com/nlp/topic-modeling-visualization-how-to-present-results-lda-models/)创建一个数据表,而不是仅仅找到一个最主要的主题,我想扩展以找到前 3 个最主要的主题,以及它们的贡献百分比和主题关键字。
为此,最好创建 2 个附加函数来创建 3 个单独的数据框,并附加每个结果?或者有没有更简单的方法来修改 format_topics_sentence 函数以从枚举的词库中提取前 3 个主题?
def format_topics_sentences(ldamodel=None, corpus=corpus, texts=data):
# Init output
sent_topics_df = pd.DataFrame()
# Get main topic in each document
for i, row_list in enumerate(ldamodel[corpus]):
row = row_list[0] if ldamodel.per_word_topics else row_list
# print(row)
row = sorted(row, key=lambda x: (x[1]), reverse=True)
# Get the Dominant topic, Perc Contribution and Keywords for each document
for j, (topic_num, prop_topic) in enumerate(row):
if j == 0: # => dominant topic
wp = ldamodel.show_topic(topic_num)
topic_keywords = ", ".join([word for word, prop in wp])
sent_topics_df = sent_topics_df.append(pd.Series([int(topic_num), round(prop_topic,4), topic_keywords]), ignore_index=True)
else:
break
sent_topics_df.columns = ['Dominant_Topic', 'Perc_Contribution', 'Topic_Keywords']
# Add original text to the end of the output
contents = pd.Series(texts)
sent_topics_df = pd.concat([sent_topics_df, contents], axis=1)
return(sent_topics_df)
df_topic_sents_keywords = format_topics_sentences(ldamodel=lda_model, corpus=corpus, texts=data_ready)
# Format
df_dominant_topic = df_topic_sents_keywords.reset_index()
df_dominant_topic.columns = ['Document_No', 'Dominant_Topic', 'Topic_Perc_Contrib', 'Keywords', 'Text']
df_dominant_topic.head(10)
解决方案
我在最近的一个项目中也有类似的要求,希望对您有所帮助,您需要在下面的代码中添加主题关键字:
topics_df1 = pd.DataFrame()
topics_df2 = pd.DataFrame()
topics_df3 = pd.DataFrame()
for i, row_list in enumerate(lda_model[corpus]):
row = row_list[0] if lda_model.per_word_topics else row_list
row = sorted(row, key=lambda x: (x[1]), reverse=True)
for j, (topic_num, prop_topic) in enumerate(row):
if len(row) >= 3:
if j ==0:
topics_df1 = topics_df1.append(pd.Series([int(topic_num), prop_topic]), ignore_index=True)
elif j ==1:
topics_df2 = topics_df2.append(pd.Series([int(topic_num), prop_topic]), ignore_index=True)
elif j ==2:
topics_df3 = topics_df3.append(pd.Series([int(topic_num), prop_topic]), ignore_index=True)
else:
break
elif len(row) == 2:
if j ==0:
topics_df1 = topics_df1.append(pd.Series([int(topic_num), prop_topic]), ignore_index=True)
elif j ==1:
topics_df2 = topics_df2.append(pd.Series([int(topic_num), prop_topic]), ignore_index=True)
topics_df3 = topics_df3.append(pd.Series(['-', '-']), ignore_index=True)
elif len(row) == 1:
topics_df1 = topics_df1.append(pd.Series([int(topic_num), prop_topic]), ignore_index=True)
topics_df2 = topics_df2.append(pd.Series(['-', '-']), ignore_index=True)
topics_df3 = topics_df3.append(pd.Series(['-', '-']), ignore_index=True)
topics_df1.rename(columns={0:'1st Topic', 1:'1st Topic Contribution'}, inplace=True)
topics_df2.rename(columns={0:'2nd Topic', 1:'2nd Topic Contribution'}, inplace=True)
topics_df3.rename(columns={0:'3rd Topic', 1:'3rd Topic Contribution'}, inplace=True)
topics_comb = pd.concat([topics_df1, topics_df2, topics_df3], axis=1, sort=False)
#Join topics dataframe to original data
new_df = pd.concat([data_ready, topics_comb], axis=1, sort=False)
推荐阅读
- javafx - Javafx 使用堆栈窗格更改窗格
- c - 使用 arm-none-eabi 工具链时重新定义 fputc 函数
- angularjs - 无法从 AngularJS Scope 中获取价值
- java - 使用媒体播放器播放 .M3U 链接
- c# - 如何使用日期范围获取目录中的文件?
- nlog - Xml 配置中的 NLog default-target-parameters 不适用于以编程方式添加的文件目标
- python-3.x - Boto3 - get_change()、change_resource_record_set() INSYNC 需要 60 秒才能返回
- javascript - 使用函数访问类方法
- python - 从模型创建表单中的相关模型字段中删除“删除选定模型”按钮(Django 管理员)
- c# - 如何将公共(全局)变量分配给 SwitchCell 的 OnProperty(isOn 绑定)以检查是否启用了 SwichCell(打开)