-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpca.py
237 lines (179 loc) · 6.46 KB
/
pca.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
__author__ = 'lqrz'
import os
import sys
from sklearn.decomposition import PCA as sklearnPCA
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d import proj3d
import numpy as np
import pickle
import random
from nltk import FreqDist
from nltk.corpus import PlaintextCorpusReader
import gensim
from tsne import tsne
import numpy as Math
import pylab
import codecs
def plotGraph(samples, word, dimensions):
if dimensions == '2D':
sklearn_pca = sklearnPCA(n_components=2)
sklearn_transf = sklearn_pca.fit_transform(samples)
plt.plot(sklearn_transf[:,0],sklearn_transf[:,1],\
'o', markersize=7, color='blue', alpha=0.5, label='')
# plt.plot(sklearn_transf[1::2,0], sklearn_transf[1::2,1],\
# '^', markersize=7, color='red', alpha=0.5, label='Matrix')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
# plt.xlim([-4,4])
plt.ylim([-.8,.8])
plt.legend()
plt.title('Word embeddings PCA')
print sklearn_transf
elif dimensions == '3D':
sklearn_pca = sklearnPCA(n_components=3)
sklearn_transf = sklearn_pca.fit_transform(samples)
fig = plt.figure(figsize=(8,8))
ax = fig.add_subplot(111, projection='3d')
plt.rcParams['legend.fontsize'] = 10
ax.plot(sklearn_transf[:,0], sklearn_transf[:,1],\
sklearn_transf[:,2], 'o', markersize=8, color='blue', alpha=0.5, label='')
# ax.plot(sklearn_transf[:,0], sklearn_transf[:,1],\
# sklearn_transf[:,2], '^', markersize=8, alpha=0.5, color='red', label='Matrix')
plt.title('Word embeddings PCA')
ax.legend(loc='upper right')
print sklearn_transf
plt.savefig("%s-%s.png" % (word, dimensions), bbox_inches='tight', dpi=200)
plt.close()
return True
def constructSamplesAndPlot(word1, tail1, word2, tail2, model):
layerSize = model.layer1_size
x = np.empty((0, layerSize))
word1Vector = model[word1]
tailVector1 = model[tail1]
word2Vector = model[word2]
tailVector2 = model[tail2]
diferenceVector = word2Vector - tailVector2
constructedVector = diferenceVector + tailVector1
idx = word2.find(tail2.lower())
if idx == -1:
print 'Error.'
exit()
head2 = word2[:idx]
head2Vector = model[head2]
x = np.r_[x, word1Vector[np.newaxis,:]]
x = np.r_[x, tailVector1[np.newaxis,:]]
x = np.r_[x, word2Vector[np.newaxis,:]]
x = np.r_[x, tailVector2[np.newaxis,:]]
x = np.r_[x, constructedVector[np.newaxis,:]]
x = np.r_[x, diferenceVector[np.newaxis,:]]
x = np.r_[x, head2Vector[np.newaxis,:]]
plotGraph(x, word1, dimensions='2D')
plotGraph(x, word1, dimensions='3D')
return True
def loadW2VModel(path):
return gensim.models.Word2Vec.load_word2vec_format(path, binary=True)
def plotPca():
# n_components = 3
modelPathDe = '../NLP2-Project2/models/mono_800_de.bin'
# Load word2vec trained models
print('Loading word2vec models...')
model = loadW2VModel(modelPathDe)
word1 = 'Hauptbahnhof'
tail1 = 'Bahnhof'
word2 = 'Hauptstadt'
tail2 = 'Stadt'
words = dict()
words['word1'] = word1
words['tail1'] = tail1
words['word2'] = word2
words['tail2'] = tail2
words['model'] = model
constructSamplesAndPlot(**words)
def plotTsne():
w2vThreshold = 2
filenames = ['Haupt.txt', 'Super.txt', 'Kinder.txt', 'Bundes.txt', 'Finanz.txt']
# filenames = ['Haupt.txt', 'Bundes.txt']
w2vPath = '../NLP2-Project2/models/mono_500_de.bin'
# w2vPath = '../NLP2-Project2/models/mono_200_de.bin'
dimensions = 500
# dimensions = 200
colours = ['#f02720', '#ff7f0f', '#32a251', '#1f77b4', '#ab6ad5']
words = set()
rawLabels = []
for i, fname in enumerate(filenames):
f = codecs.open(fname, 'rb', encoding='utf-8')
for l in f:
clean = l.strip().split(' ')
if clean[0] > w2vThreshold:
words.add(clean[1])
rawLabels.append(colours[i])
model = loadW2VModel(w2vPath)
X = Math.empty((0, dimensions))
# labels = Math.empty((1),dtype=float)
labels = []
for i,w in enumerate(words):
try:
rep = model[w]
X = Math.r_[X, rep[Math.newaxis,:]]
labels.append(rawLabels[i])
except KeyError:
continue
# X = Math.loadtxt()
# labels = Math.loadtxt()
Y = tsne(X, 2, dimensions, 20.0, max_iter=1000)
pylab.scatter(Y[:,0], Y[:,1], 18, marker='o', c=labels, edgecolor='None')
pylab.savefig('scatter.png')
# pylab.show()
def plotScatterPca():
w2vThreshold = 2
prefix = True
if prefix:
filenames = ['Haupt.txt', 'Bundes.txt', 'Super.txt', 'Finanz.txt']
else:
filenames = ['Arbeit.txt', 'Mann.txt', 'Ministerium.txt', 'Stadt.txt']
w2vPath = 'models/mono_500_de.bin'
dimensions = 500
colours = ['#f02720', '#ff7f0f', '#32a251', '#1f77b4', '#ab6ad5']
words = []
rawLabels = []
for i, fname in enumerate(filenames):
f = codecs.open(fname, 'rb', encoding='utf-8')
for l in f:
clean = l.strip().split(' ')
if clean[0] > w2vThreshold:
words.append(clean[1])
rawLabels.append(colours[i])
model = loadW2VModel(w2vPath)
X = Math.empty((0, dimensions))
# labels = Math.empty((1),dtype=float)
labels = []
for i,w in enumerate(words):
try:
rep = model[w]
X = Math.r_[X, rep[Math.newaxis,:]]
labels.append(rawLabels[i])
except KeyError:
continue
# X = Math.loadtxt()
# labels = Math.loadtxt()
# Y = tsne(X, 2, dimensions, 20.0, max_iter=1000)
sklearn_pca = sklearnPCA(n_components=2)
sklearn_transf = sklearn_pca.fit_transform(X)
# plt.plot(sklearn_transf[:,0],sklearn_transf[:,1],\
# 'o', markersize=7, color='blue', alpha=0.5, label='')
# plt.plot(sklearn_transf[1::2,0], sklearn_transf[1::2,1],\
# '^', markersize=7, color='red', alpha=0.5, label='Matrix')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
# plt.xlim([-4,4])
# plt.ylim([-.8,.8])
plt.legend()
plt.title('Word embeddings PCA')
pylab.scatter(sklearn_transf[:,0], sklearn_transf[:,1], 18, marker='o', c=labels, edgecolor='None')
pylab.savefig('scatter'+str(prefix)+'.png')
# pylab.show()
if __name__ == '__main__':
# plotPca()
# plotTsne()
plotScatterPca()