Python LDA主题模型结果保存到Excel
如何将Python LDA主题模型结果保存到Excel
本文将介绍如何将Python LDA主题模型的运行结果保存到当前文件路径下的Excel文件中。
1. 使用pandas库保存结果
可以使用Python中的pandas库将结果保存为Excel文件。首先,将结果存储在一个DataFrame中,然后使用to_excel()方法将其保存为Excel文件。
2. 代码示例
import pandas as pd
n_topics = 5
lda.fit(tf)
tf_feature_names = tf_vectorizer.get_feature_names()
topic_word_prob = lda.components_ / lda.components_.sum(axis=1)[:, np.newaxis]
# 保存结果到DataFrame中
result_df = pd.DataFrame(columns=['Topic', 'Top Words', 'Word Prob'])
for i, topic_prob in enumerate(topic_word_prob):
top_words_idx = topic_prob.argsort()[:-21:-1]
top_words = [tf_feature_names[idx] for idx in top_words_idx]
word_prob = [str(prob) for prob in topic_prob[top_words_idx]]
result_df.loc[i] = [f'Topic {i}', ', '.join(top_words), ', '.join(word_prob)]
# 保存为Excel文件
result_df.to_excel('result.xlsx', index=False)
3. 运行结果
运行完毕后,结果将保存在当前文件路径下的名为“result.xlsx”的Excel文件中。
4. 总结
本文介绍了如何将Python LDA主题模型的运行结果保存到Excel文件中。使用pandas库可以方便地将结果存储在一个DataFrame中,然后使用to_excel()方法将其保存为Excel文件。
原文地址: https://www.cveoy.top/t/topic/m0f8 著作权归作者所有。请勿转载和采集!