-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathword2vec_graph.py
More file actions
46 lines (35 loc) · 1.3 KB
/
Copy pathword2vec_graph.py
File metadata and controls
46 lines (35 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python
# coding=utf-8
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
import word2vec
# load the word2vec model
model = word2vec.load('corpusWord2Vec.bin')
rawWordVec=model.vectors
# reduce the dimension of word vector
X_reduced = PCA(n_components=2).fit_transform(rawWordVec)
# show some word(center word) and it's similar words
index1,metrics1 = model.cosine(u'识别')
index2,metrics2 = model.cosine(u'显示卡')
index3,metrics3 = model.cosine(u'关机')
# add the index of center word
index01=np.where(model.vocab==u'识别')
index02=np.where(model.vocab==u'显示卡')
index03=np.where(model.vocab==u'关机')
index1=np.append(index1,index01)
index2=np.append(index2,index03)
index3=np.append(index3,index03)
# plot the result
zhfont = matplotlib.font_manager.FontProperties(fname='/usr/share/fonts/truetype/wqy/wqy-microhei.ttc')
fig = plt.figure()
ax = fig.add_subplot(111)
for i in index1:
ax.text(X_reduced[i][0],X_reduced[i][1], model.vocab[i], fontproperties=zhfont, color='r')
for i in index2:
ax.text(X_reduced[i][0],X_reduced[i][1], model.vocab[i], fontproperties=zhfont, color='b')
for i in index3:
ax.text(X_reduced[i][0],X_reduced[i][1], model.vocab[i], fontproperties=zhfont, color='g')
ax.axis([-1,1,-0.5,1])
plt.show()