我是 python 和 ML 的新手。我找到了一个很好的脚本(https://www.machinelearningplus.com/nlp/topic-modeling-visualization-how-to-present-results-lda-models/)关于如何为 LDA 和我的每个文档获取属性主题将其更改为也可以与 LSI 一起使用。原始代码是:
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']
为了将它用于 LSI,我将其更改为:
def format_topics_sentences_lsi(LsiModel=None, corpus=corpus, texts=data):
"""
Extract all the information needed such as most predominant topic assigned to document and percentage of contribution
LsiModel= model to be used
corpus = corpus to be used
texts = original text to be classify (for topic assignment)
"""
# Init output
sent_topics_df = pd.DataFrame()
# Get main topic in each document
for i, row in enumerate(LsiModel[corpus]):
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 = LsiModel.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']
- 这是正确的方法吗?
- 由于 LSI 不是基于概率,因此“Perc_Contrib”高于 100%。我应该如何解释这个数字?
- 除了上面的脚本,由于 LSI 没有 get_document_topics,我可以使用哪个函数来查看得分最高的主题?