-
Notifications
You must be signed in to change notification settings - Fork 0
/
Visualizations of training.py
211 lines (157 loc) · 6.25 KB
/
Visualizations of training.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
import random
import pandas as pd
import re
import copy
import numpy as np
############################
#getting the list of persons alteady there in the old train files
path = "C:\\Sharan\\CS 412\\cs 412 proj\\7th 8th\\radicchio-399581f9006c39a5b4a2dc0a91f818db3a7bc398\\data\\"
fil = path + "profession.train"
persons_prof_df = pd.read_csv(fil, sep='\t',header=None)
persons_prof_df.columns=['Person','profession','score']
persons_prof=list(set(persons_prof_df['Person']))
fil = path + "nationality.train"
persons_nation_df = pd.read_csv(fil, sep='\t',header=None)
persons_nation_df.columns=['Person','nationality','score']
persons_nat=list(set(persons_nation_df['Person']))
#########################
#we need to normalize the stuff... lowercase and remove punctuations,
def normalize(text):
text = re.sub(r'[^\w\s]' , " ", text, re.UNICODE)#remove punctuations
text = text.lower()
#terms = text.split()# the splitting will be done later
return text
variables1=['persons_prof','persons_nat']
for v in variables1:
for i in range(0,len(globals()[v]),1):
globals()[v][i]=normalize(globals()[v][i])
#######
#this are arrays
persons_prof_vec=[]
persons_nat_vec=[]
variables2=['persons_prof_vec','persons_nat_vec']
zerovec=[0.0]*301
for v in range(0,2,1):
for i in range(0,len(globals()[variables1[v]]),1):
globals()[variables2[v]].append(zerovec)
persons_prof_vec=np.array(persons_prof_vec)
persons_nat_vec=np.array(persons_nat_vec)
fil = path + "vectors.txt"
c=0
# we go through each line (word) in the word vector file... then we search if that work is there in any of the files....
# if yes, we update the weights in each of the 4 files above
with open(fil) as infile:
for line in infile:
a = line.split(" ")
'''
print(a[0])
print(a[1:])
c+=1
if c>2:
break
'''
# we just convery the numerical string into float values.
#and then append the line to one of the lists if relevent
for v in range(0,2,1):
for i in range(0, len(globals()[variables1[v]]), 1):
temp=copy.deepcopy(globals()[variables1[v]][i])
row=temp.split(" ")
for rowele in row:
if a[0]==rowele:#now we add the weight vectors onto the corresponding row column 2..and increment counter in column 3
globals()[variables2[v]][i][300]+=1
print("i= " + str(i) + " and ")
for w in range(1,301,1):
float_a=float(a[w])
globals()[variables2[v]][i][w-1] += float_a
c=c+1
if c % 100 ==0:
print(c)
for v in range(0,2,1):
l=len(globals()[variables2[v]])
i=0
while(i<l):
if globals()[variables2[v]][i][300]==0:
globals()[variables2[v]] = np.delete(globals()[variables2[v]],(i),axis=0)
del globals()[variables1[v]][i]
l = len(globals()[variables2[v]])
continue
for w in range(0, 300, 1):
globals()[variables2[v]][i][w] = globals()[variables2[v]][i][w] / float(globals()[variables2[v]][i][300])
i=i+1
for v in variables2:
globals()[v]=np.delete(globals()[v],(300),axis=1)
##########################################################################################
## now we just reduce the dimension of the persons vector from 300 d to 2 d and plot it ##
##########################################################################################
#from scikit-learn.manifold import tsne
#matplot lib renders the output through some other package.... and in this case it was looking for PyQt5 which is not there on my machone...
#so when u change the backend rendering package that matplotlib uses to TkAgg(ie matplotlib.use(TkAgg))
#it then is able to use matplot lib
#matplotlib.use('TkAgg')
import matplotlib
import matplotlib.pyplot as plt#error
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.ticker import NullFormatter
from sklearn import manifold
X=persons_prof_vec
n_components = 2
#2 dimentios
#fit transform
tsne = manifold.TSNE(n_components=n_components, init='pca', random_state=0)
Y = tsne.fit_transform(X)
import numpy as np
import matplotlib.pyplot as plt
import random
N = len(Y)
data = Y
labels = persons_prof
plt.subplots_adjust(bottom = 0.1)
plt.scatter(
data[:,0], data[:, 1], marker = 'o',
cmap = plt.get_cmap('Spectral'))
'''
#RANDOM SUBSETTING:
#now we label the points... we need to label only one in 4 or 5 points... not all
subset=[]
for i in range(0,N,1):
subset.append(i)
subsetlist=random.sample(subset,round(len(subset)/4))
subsetdataX=[]
subsetdataY=[]
subsetlabels=[]
for i in range(0,len(subsetlist),1):
subsetdataX.append(Y[subsetlist[i]][0])
subsetdataY.append(Y[subsetlist[i]][1])
subsetlabels.append(labels[subsetlist[i]])
'''
#ONLY SUBSETTING THOSE which are of same profession and score combo (just change the if condition>>>)
per_list=list(persons_prof_df['Person'])
prof_list=list(persons_prof_df['profession'])
score_list=list(persons_prof_df['score'])
indices=[]
for i in range(0,len(per_list),1):
#if prof_list[i]=="Author":# and score_list[i]==7:
if prof_list[i] == "Author" and score_list[i] == 7:
indices.append(i)
subsetlist=[]
for p in indices:
normalized_per=normalize(per_list[p])
ind=persons_prof.index(normalized_per)
subsetlist.append(ind)
subsetlist=list(set(subsetlist))
subsetdataX=[]
subsetdataY=[]
subsetlabels=[]
for i in range(0,len(subsetlist),1):
subsetdataX.append(Y[subsetlist[i]][0])
subsetdataY.append(Y[subsetlist[i]][1])
subsetlabels.append(labels[subsetlist[i]])
#for label, x, y in zip(labels, data[:, 0], data[:, 1]):
for label, x, y in zip(subsetlabels, subsetdataX, subsetdataY):
plt.annotate(
label,
xy = (x, y), xytext = (-10, 10),
textcoords = 'offset points', ha = 'right', va = 'bottom',
#bbox = dict(boxstyle = 'round,pad=0.5', fc = 'grey', alpha = 0.5),
arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0'))
plt.show()