gpt4 book ai didi

python - 将 pyLDAvis 图导出为 pdf

转载 作者:行者123 更新时间:2023-12-04 15:20:44 27 4
gpt4 key购买 nike

我正在用 Python 进行 LDA 主题建模,以下是我的可视化代码:

import pyLDAvis.gensim
pyLDAvis.enable_notebook()
vis = pyLDAvis.gensim.prepare(lda_model, corpus, dictionary=lda_model.id2word)
vis

我正在寻找一种将主题间距离图导出为 PDF 或至少使用 matplotlib 绘制它然后另存为 pdf 的方法,有什么想法吗?

最佳答案

您可以将模型导出为 JSON 格式,然后与 matplotlib 一起使用

# Export results in JSON format

pyLDAvis.enable_notebook()
vis = pyLDAvis.gensim.prepare(lda_model, corpus, id2word)
vis
pyLDAvis.save_json(vis, '/results/lda.json')

# Read JSON file

import json

with open('/results/lda.json', 'r') as myfile:
data=myfile.read()

json_data = json.loads(data)


# Plot with matplotlib

import matplotlib.pyplot as plt

x_max = max(json_data['mdsDat']['x']) + (max(json_data['mdsDat']['x']) - min(json_data['mdsDat']['x']))
y_max = max(json_data['mdsDat']['y']) + (max(json_data['mdsDat']['y']) - min(json_data['mdsDat']['y']))
x_min = min(json_data['mdsDat']['x']) - (max(json_data['mdsDat']['x']) - min(json_data['mdsDat']['x']))
y_min = min(json_data['mdsDat']['y']) - (max(json_data['mdsDat']['y']) - min(json_data['mdsDat']['y']))

plt.axis([x_min, x_max, y_min, y_max])

# Depending on the number of topics, you may need to tweak the paremeters (e.g. the size of circles be Freq/100 or Freq/200, etc)

for i in range(len(json_data['mdsDat']['x'])):
circle = plt.Circle((json_data['mdsDat']['x'][i],json_data['mdsDat']['y'][i]), radius = json_data['mdsDat']['Freq'][i]/100)
plt.gca().add_artist(circle)

plt.show()

关于python - 将 pyLDAvis 图导出为 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63362101/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com