为Gensim模型绘制3D图

时间:2020-02-28 15:05:28

标签: python-3.x pca gensim

我已经使用Gensim训练了模型。我使用PCA绘制了2D绘图,但不清楚。我想将其更改为具有缩放功能的3D。我的结果是如此密集。

from sklearn.decomposition import PCA
from matplotlib import pyplot
X=model[model.wv.vocab]
pca=PCA(n_components=2)
result=pca.fit_transform(X)
pyplot.scatter(result[:,0],result[:,1])
word=list(model.wv.most_similar('eden_lake'))
for i, word in enumerate(words):
  pyplot.annotate(word, xy=(result[i, 0], result[i, 1]))
pyplot.show()

结果: enter image description here

有可能做到吗?

2 个答案:

答案 0 :(得分:0)

是的,原则上可以对LDA模型结果进行3D可视化。 Here是有关使用T-SNE的更多信息。

答案 1 :(得分:0)

以下功能使用t-SNE而不是PCA进行尺寸缩减,但是将生成二维,三维或二维和三维图(使用子图)。此外,它将为您着色主题,因此更容易区分它们。将%matplotlib notebook添加到anaconda到Jupyter笔记本环境的开始处将允许旋转3d图和缩放2d图(不要同时使用{{1 }}。

该函数很长,大多数代码用于绘图格式设置,但是会产生专业的输出。

%matplotlib notebook
import numpy as np
import pandas as pd

import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
import seaborn as sns

from gensim.models import LdaModel
from gensim import corpora
from sklearn.manifold import TSNE
# %matplotlib notebook # if in Jupyter for rotating and zooming

在子图中10个主题gensim LDA模型的2d和3d表示的示例图(除去了异常值)将是:

LDA t-SNE Dimensional Reduction Plots