n_topics = 10 lda = LatentDirichletAllocation(n_components=n_topics, max_iter=50, learning_method='batch', learning_offset=50, #doc_topic_prior=0.1, #topic_word_prior=0.01, random_state=0) lda.fit(tf) ###########每个主题对应词语 import pandas as pd from openpyxl import Workbook # 获取主题下词语的概率分布 def get_topic_word_distribution(lda, tf_feature_names): arr = lda.transform(tf_vectorizer.transform([' '.join(tf_feature_names)])) return arr[0] # 打印主题下词语的概率分布 def print_topic_word_distribution(lda, tf_feature_names, n_top_words): dist = get_topic_word_distribution(lda, tf_feature_names) for i in range(lda.n_topics): print("Topic {}: {}".format(i, ', '.join("{:.4f}".format(x) for x in dist[i]))) # 输出每个主题下词语的概率分布至Excel表格 def output_topic_word_distribution_to_excel(lda, tf_feature_names, n_top_words, filename): # 创建Excel工作簿和工作表 wb = Workbook() ws = wb.active ws.title = "Topic Word Distribution" # 添加表头 ws.cell(row=1, column=1).value = "Topic" for j in range(n_top_words): ws.cell(row=1, column=j+2).value = tf_feature_names[j] # 添加每个主题下词语的概率分布 dist = get_topic_word_distribution(lda, tf_feature_names) for i in range(lda.n_topics): ws.cell(row=i+2, column=1).value = i for j in range(n_top_words): ws.cell(row=i+2, column=j+2).value = dist[i][j] # 保存Excel文件 wb.save(filename) n_top_words = 30 tf_feature_names = tf_vectorizer.get_feature_names() topic_word = print_topic_word_distribution(lda, tf_feature_names, n_top_words) #print_topic_word_distribution(lda, tf_feature_names, n_top_words) output_topic_word_distribution_to_excel(lda, tf_feature_names, n_top_words, "topic_word_distribution.xlsx")报错Traceback (most recent call last): File "D:\python\lda3\data_1.py", line 157, in <module> topic_word = print_topic_word_distribution(lda, tf_feature_names, n_top_words) File "D:\python\lda3\data_1.py", line 129, in print_topic_word_distribution for i in range(lda.n_topics): AttributeError: 'LatentDirichletAllocation' object has no attribute 'n_topics'
时间: 2023-12-20 07:03:15 浏览: 136
这个错误提示的意思是说 `LatentDirichletAllocation` 对象没有 `n_topics` 这个属性。你可以尝试将 `n_topics` 直接传递给函数,而不是从 `lda` 对象中获取。修改代码如下:
```
def print_topic_word_distribution(lda, tf_feature_names, n_top_words, n_topics):
dist = get_topic_word_distribution(lda, tf_feature_names, n_topics)
for i in range(n_topics):
print("Topic {}: {}".format(i, ', '.join("{:.4f}".format(x) for x in dist[i])))
def output_topic_word_distribution_to_excel(lda, tf_feature_names, n_top_words, n_topics, filename):
# 创建Excel工作簿和工作表
wb = Workbook()
ws = wb.active
ws.title = "Topic Word Distribution"
# 添加表头
ws.cell(row=1, column=1).value = "Topic"
for j in range(n_top_words):
ws.cell(row=1, column=j+2).value = tf_feature_names[j]
# 添加每个主题下词语的概率分布
dist = get_topic_word_distribution(lda, tf_feature_names, n_topics)
for i in range(n_topics):
ws.cell(row=i+2, column=1).value = i
for j in range(n_top_words):
ws.cell(row=i+2, column=j+2).value = dist[i][j]
# 保存Excel文件
wb.save(filename)
n_top_words = 30
n_topics = 10
tf_feature_names = tf_vectorizer.get_feature_names()
topic_word = print_topic_word_distribution(lda, tf_feature_names, n_top_words, n_topics)
output_topic_word_distribution_to_excel(lda, tf_feature_names, n_top_words, n_topics, "topic_word_distribution.xlsx")
```
这样应该就能够正常执行了。
阅读全文