卯卯 | 炼就一手绝世刀法!

日出东海落西山,愁也一天,喜也一天。遇事不钻牛角尖,人也舒坦,心也舒坦!

gensim训练word2vec并使用PCA实现二维可视化

image.png

结果:

image.png

代码:

# -*- coding: utf-8 -*-
from gensim.models import Word2Vec
from sklearn.decomposition import PCA
from matplotlib import pyplot
# 训练的语料
sentences = [['this', 'is', 'the', 'first', 'sentence', 'for', 'word2vec'],
   ['this', 'is', 'the', 'second', 'sentence'],
   ['yet', 'another', 'sentence'],
   ['one', 'more', 'sentence'],
   ['and', 'the', 'final', 'sentence']]
# 利用语料训练模型
model = Word2Vec(sentences,window=5, min_count=1)
#添加vocab:
model.build_vocab(sentences, update=True) #添加vocab
#check是否添加成功:
for k,v in model.wv.vocab.items():
    print (k,v)
# 基于2d PCA拟合数据
X = model[model.wv.vocab]
pca = PCA(n_components=2)##PCA无监督的降维,LDA线性判别分析:有监督的降维
result = pca.fit_transform(X)
print(result)
print(result[:, 0])
# 可视化展示
pyplot.scatter(result[:, 0], result[:, 1])##scatter:散开,分散,两个点
words = list(model.wv.vocab)
for i, word in enumerate(words):
 pyplot.annotate(word, xy=(result[i, 0], result[i, 1]))##annotate:注释
pyplot.show()




发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

«   2025年4月   »
123456
78910111213
14151617181920
21222324252627
282930
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
搜索
最新留言
文章归档
网站收藏
友情链接
  • RainbowSoft Studio Z-Blog
  • 订阅本站的 RSS 2.0 新闻聚合

Powered By Z-BlogPHP 1.5.2 Zero

转载请注明文章出处!!!!!