将pyLDAvis图表导出为PDF文件。

3

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

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

我正在寻找一种将Intertopic Distance Map图形导出为PDF格式的方法,或者至少使用matplotlib绘制然后保存为pdf,有什么建议吗?

1个回答

2

你可以将模型导出为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()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接