import matplotlibpyplot as plt plexs = scores = n_max_topics = 16 # 这里值可自行设置for i in range1n_max_topics lda = LatentDirichletAllocationn_components=i max_iter=50
import matplotlib.pyplot as plt
创建两个空列表,用于存储 perplexity 和 score
plexs = [] scores = []
设置最大的主题数
n_max_topics = 16
循环从1到n_max_topics,依次计算 perplexity 和 score
for i in range(1, n_max_topics): # 创建一个 LatentDirichletAllocation 对象,设置主题数为 i lda = LatentDirichletAllocation(n_components=i, max_iter=50, learning_method='batch', learning_offset=50, random_state=666)
# 使用 tf 训练 LDA 模型
lda.fit(tf)
# 计算 perplexity 和 score,并将结果添加到对应的列表中
plexs.append(lda.perplexity(tf))
scores.append(lda.score(tf))
设置区间最右侧的值
n_t = 15
创建 x 轴的取值范围
x = list(range(1, n_t))
绘制 perplexity 和主题数的关系图
plt.plot(x, plexs[1:n_t]) plt.xlabel("number of topics") plt.ylabel("perplexity") plt.show()
解释:
- 第一行导入了 matplotlib.pyplot 库,用于绘制图表。
- 第三行和第四行创建了两个空列表,用于存储 perplexity 和 score。
- 第七行设置最大的主题数。
- 第十行到第二十行使用循环从1到n_max_topics,依次计算 perplexity 和 score。
- 第十三行创建了一个 LatentDirichletAllocation 对象,设置主题数为 i。
- 第十六行使用 tf 训练 LDA 模型。
- 第十九行和第二十行分别计算 perplexity 和 score,并将结果添加到对应的列表中。
- 第二十三行设置区间最右侧的值。
- 第二十六行创建 x 轴的取值范围。
- 第二十九行绘制 perplexity 和主题数的关系图。
原文地址: https://www.cveoy.top/t/topic/i3iS 著作权归作者所有。请勿转载和采集!