【发布时间】:2020-12-04 07:50:29
【问题描述】:
我正在尝试通过使用电影评论的公共数据集来熟悉 Doc2Vec 结果。我已经清理了数据并运行了模型。如下所示,有 6 个标签/流派。每个都是具有其矢量表示的文档。
doc_tags = list(doc2vec_model.docvecs.doctags.keys())
print(doc_tags)
X = doc2vec_model[doc_tags]
print(X)
['animation', 'fantasy', 'comedy', 'action', 'romance', 'sci-fi']
[[ -0.6630892 0.20754902 0.2949621 0.622197 0.15592825]
[ -1.0809666 0.64607996 0.3626246 0.9261689 0.31883526]
[ -2.3482993 2.410015 0.86162883 3.0468733 -0.3903969 ]
[ -1.7452248 0.25237766 0.6007084 2.2371168 0.9400951 ]
[ -1.9570891 1.3037877 -0.24805197 1.6109428 -0.3572465 ]
[-15.548988 -4.129228 3.608777 -0.10240117 3.2107658 ]]
print(doc2vec_model.docvecs.most_similar('romance'))
[('comedy', 0.6839742660522461), ('animation', 0.6497607827186584), ('fantasy', 0.5627620220184326), ('sci-fi', 0.14199887216091156), ('action', 0.046558648347854614)]
“浪漫”和“喜剧”非常相似,而“动作”和“科幻”与“浪漫”相比是完全不同的类型。到目前为止,一切都很好。但是,为了可视化结果,我需要降低向量维数。因此,我先尝试 t-SNE,然后尝试 PCA。这是代码和结果:
# TSNE
tsne = TSNE(n_components=2)
X_tsne = tsne.fit_transform(X)
df = pd.DataFrame(X_tsne, index=doc_tags, columns=['x', 'y'])
print(df)
x y
animation -162.499695 74.153679
fantasy -10.496888 93.687149
comedy -38.886723 -56.914558
action -76.036247 232.218231
romance 101.005371 198.827988
sci-fi 123.960182 20.141081
# PCA
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X)
df_1 = pd.DataFrame(X_pca, index=doc_tags, columns=['x', 'y'])
print(df_1)
x y
animation -3.060287 -1.474442
fantasy -2.815175 -0.888522
comedy -2.520171 2.244404
action -2.063809 -0.191137
romance -2.578774 0.370727
sci-fi 13.038214 -0.061030
出了点问题。当我将结果可视化时,这一点更加明显:
TSNE:
主成分分析:
这显然不是模型所产生的。我确定我缺少一些基本的东西。如果您有任何建议,将不胜感激。
【问题讨论】:
标签: python machine-learning scatter-plot cosine-similarity doc2vec