-
Notifications
You must be signed in to change notification settings - Fork 24
/
pca_ppa.py
58 lines (45 loc) · 1.64 KB
/
pca_ppa.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
import numpy as np
import cPickle as pickle
from sklearn.decomposition import PCA
import subprocess
Glove = {}
f = open('/home/vikas/Desktop/glove.6B/glove.6B.300d.txt')
print("Loading Glove vectors.")
for line in f:
values = line.split()
word = values[0]
coefs = np.asarray(values[1:], dtype='float32')
Glove[word] = coefs
f.close()
print("Done.")
X_train = []
X_train_names = []
for x in Glove:
X_train.append(Glove[x])
X_train_names.append(x)
X_train = np.asarray(X_train)
pca_embeddings = {}
# PCA Dim Reduction
pca = PCA(n_components = 150)
X_train = X_train - np.mean(X_train)
X_new_final = pca.fit_transform(X_train)
# PCA to do Post-Processing
pca = PCA(n_components = 150)
X_new = X_new_final - np.mean(X_new_final)
X_new = pca.fit_transform(X_new)
Ufit = pca.components_
X_new_final = X_new_final - np.mean(X_new_final)
final_pca_embeddings = {}
embedding_file = open('pca_embed2.txt', 'w')
for i, x in enumerate(X_train_names):
final_pca_embeddings[x] = X_new_final[i]
embedding_file.write("%s\t" % x)
for u in Ufit[0:7]:
final_pca_embeddings[x] = final_pca_embeddings[x] - np.dot(u.transpose(),final_pca_embeddings[x]) * u
for t in final_pca_embeddings[x]:
embedding_file.write("%f\t" % t)
embedding_file.write("\n")
print("Results for the Embedding")
print subprocess.check_output(["python", "all_wordsim.py", "pca_embed2.txt", "data/word-sim/"])
print("Results for Glove")
print subprocess.check_output(["python", "all_wordsim.py", "../glove.6B/glove.6B.300d.txt", "data/word-sim/"])