我试图使用python中的日志困惑来确定我的LDA模型的最佳主题数量。也就是说,我正在绘制一系列主题的日志困惑,并确定最低限度的困惑。然而,我得到的图对于日志困惑有负值,当它应该有0到1之间的正值时。
#calculating the log perplexity per word as obtained by gensim code
##https://radimrehurek.com/gensim/models/atmodel.html
#parameters: pass in trained corpus
#return: graph of perplexity per word for varying number of topics
parameter_list = range(1, 500, 100)
grid ={}
for parameter_value in parameter_list:
model = models.LdaMulticore(corpus=corpus, workers=None, id2word=None,
num_topics=parameter_value, iterations=10)
grid[parameter_value]=[]
perplex=model.log_perplexity(corpus, total_docs=len(corpus))
grid[parameter_value].append(perplex)
df = pd.DataFrame(grid)
ax = plt.figure(figsize=(7, 4), dpi=300).add_subplot(111)
df.iloc[0].transpose().plot(ax=ax, color="#254F09")
plt.xlim(parameter_list[0], parameter_list[-1])
plt.ylabel('Perplexity')
plt.xlabel('topics')
plt.show() 发布于 2021-11-02 18:09:27
困惑必须介于0到1之间。它是负的,因为(0,1)范围内的一个数字的对数低于零。
https://stackoverflow.com/questions/44787568
复制相似问题