我似乎找不到它,或者我的统计知识及其术语可能是这里的问题,但我想实现类似于PyPI 的 LDA lib底部页面上的图表并观察一致性/收敛性线。如何使用Gensim LDA实现这一目标?
问问题
6176 次
1 回答
14
您希望绘制模型拟合的收敛性是正确的。不幸的是,Gensim 似乎并没有把这件事做得很直截了当。
以能够分析模型拟合函数的输出的方式运行模型。我喜欢设置一个日志文件。
import logging logging.basicConfig(filename='gensim.log', format="%(asctime)s:%(levelname)s:%(message)s", level=logging.INFO)
中设置
eval_every
参数LdaModel
。该值越低,绘图的分辨率就越高。但是,计算困惑度会大大降低您的适应度!lda_model = LdaModel(corpus=corpus, id2word=id2word, num_topics=30, eval_every=10, pass=40, iterations=5000)
解析日志文件并制作你的情节。
import re import matplotlib.pyplot as plt p = re.compile("(-*\d+\.\d+) per-word .* (\d+\.\d+) perplexity") matches = [p.findall(l) for l in open('gensim.log')] matches = [m for m in matches if len(m) > 0] tuples = [t[0] for t in matches] perplexity = [float(t[1]) for t in tuples] liklihood = [float(t[0]) for t in tuples] iter = list(range(0,len(tuples)*10,10)) plt.plot(iter,liklihood,c="black") plt.ylabel("log liklihood") plt.xlabel("iteration") plt.title("Topic Model Convergence") plt.grid() plt.savefig("convergence_liklihood.pdf") plt.close()
于 2017-08-19T20:48:27.357 回答