可以使用Python中的pandas库将结果保存为Excel文件。首先,将结果存储在一个DataFrame中,然后使用to_excel()方法将其保存为Excel文件。

以下是代码示例:

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)

运行完毕后,结果将保存在当前文件路径下的名为“result.xlsx”的Excel文件中。

n_topics = 5 # 设置主题数目为5 ldafittf tf_feature_names = tf_vectorizerget_feature_names topic_word_prob = ldacomponents_ ldacomponents_sumaxis=1 npnewaxis for i topic_prob in enumeratetopic_word_prob

原文地址: https://www.cveoy.top/t/topic/bwoG 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录