-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignment3_raquelaoki.py
384 lines (309 loc) · 13.3 KB
/
assignment3_raquelaoki.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#Class: Machine Learning - SFU - Fall 2017
#Assigment 3
#Author: Raquel Aoki
#Date: 2017/11/17
import numpy
import pandas
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
import scipy.special as sps
import matplotlib.pyplot as plt
#reading dataset
pandas.set_option('display.max_columns', None)
#Data Preprocessing
print('\n Data Preprocessing')
bd = pandas.DataFrame.from_csv('/home/raquel/Documentos/SFU/Machine Learning/preprocessed_datasets2.csv')
#bd = pandas.DataFrame.from_csv('C:\\Users\\raoki\\Downloads\\preprocessed_datasets2.csv')
resp = bd['sum_7yr_GP']
bd = bd.drop('sum_7yr_GP',axis=1)
bd = pandas.concat([resp,bd],axis=1)
bd = bd[(bd.DraftYear == 2004) | (bd.DraftYear == 2005) |(bd.DraftYear == 2006)|(bd.DraftYear == 2007)]
#drop columns that won't be used
bd=bd.drop(['PlayerName', 'Country','sum_7yr_TOI','Overall'],axis=1)
print('a) Dropping columns PlayerName, Country,sum_7yr_TOI,Overall\n')
#dummy variables
colnames_cat = ['country_group', 'Position']
dummy = pandas.get_dummies(bd[colnames_cat])
DraftYear = bd['DraftYear']
bd=bd.drop(['country_group', 'Position','DraftYear'],axis=1)
bd = pandas.concat([DraftYear,bd, dummy], axis=1)
print('b) Dummy variables to country group and position\n')
#standart
col=['DraftAge', 'Height', 'Weight', 'CSS_rank' ,'rs_GP', 'rs_G', 'rs_A' , 'rs_P', 'rs_PIM', 'rs_PlusMinus',
'po_GP', 'po_G', 'po_A', 'po_P', 'po_PIM' , 'country_group_CAN', 'country_group_EURO',
'country_group_USA', 'Position_C' , 'Position_D' , 'Position_L', 'Position_R' ]
for i in col:
bd[i]= (bd[i]-numpy.mean(bd[i]))/(numpy.sqrt(numpy.var(bd[i])))
#Question 2
print('Question 2\n')
print('\n\n.... Split into training and testing set + dropping Draftyear\n\n')
##dropping draftyear
train = bd[(bd.DraftYear == 2004) | (bd.DraftYear == 2005) |(bd.DraftYear == 2006)]
test = bd[bd.DraftYear == 2007]
train = train.drop(['DraftYear','sum_7yr_GP'],axis=1)
test = test.drop(['DraftYear','sum_7yr_GP'],axis=1)
GP_greater_than_0 = train['GP_greater_than_0']
#train=train.drop(['GP_greater_than_0','po_GP', 'po_G', 'po_A', 'po_P', 'po_PIM'],axis=1)
train=train.drop(['GP_greater_than_0'],axis=1)
col1 = pandas.Series(numpy.zeros((train.shape[0]))+1, index = train.index)
train = pandas.concat([col1,train,GP_greater_than_0], axis=1)
GP_greater_than_0 = test['GP_greater_than_0']
#test=test.drop(['GP_greater_than_0','po_GP', 'po_G', 'po_A', 'po_P', 'po_PIM'],axis=1)
test=test.drop(['GP_greater_than_0'],axis=1)
col1 = pandas.Series(numpy.zeros((test.shape[0]))+1, index = test.index)
test = pandas.concat([col1,test,GP_greater_than_0], axis=1)
train['GP_greater_than_0'] = train['GP_greater_than_0'].replace('no',0)
train['GP_greater_than_0'] = train['GP_greater_than_0'].replace('yes',1)
test['GP_greater_than_0'] = test['GP_greater_than_0'].replace('no',0)
test['GP_greater_than_0'] = test['GP_greater_than_0'].replace('yes',1)
# Maximum number of iterations. Continue until this limit, or when error change is below tol.
max_iter = 500
tol = 0.00001
# Step size for gradient descent.
etas = [0.5, 0.3, 0.1, 0.05, 0.01]
numpy.random.seed(42)
train = numpy.random.permutation(train)
test = numpy.random.permutation(test)
# Data matrix, with column of ones at end.
X = train[:,0:23] #23
Xtest = test[:,0:23] #23
# Target values, 0 for class 1, 1 for class 2.
t = train[:,23]
ttest = test[:,23]
# For plotting data
class1 = numpy.where(t==0)
X1 = X[class1]
class2 = numpy.where(t==1)
X2 = X[class2]
n_train = t.size
# Error values over all iterations.
all_errors = dict()
accuracy = []
for eta in etas:
# Initialize w.
w = numpy.zeros((23))
w[0] = w[0]+0.1
e_all = []
for iter in range (0, max_iter):
for n in range (0, n_train):
# Compute output using current w on sample x_n.
y = sps.expit(numpy.dot(X[n,:],w))
# Gradient of the error, using Assignment result
grad_e = (y - t[n])*X[n,:]
# Update w, *subtracting* a step in the error derivative since we're minimizing
w = w - eta*grad_e
# Compute error over all examples, add this error to the end of error vector.
# Compute output using current w on all data X.
y = sps.expit(numpy.dot(X,w))
# e is the error, negative log-likelihood (Eqn 4.90)
e = -numpy.mean(numpy.multiply(t,numpy.log(y)) + numpy.multiply((1-t),numpy.log(1-y)))
#e = -(sum(t[t==1]*numpy.log(y[t==1]))+ sum(numpy.log(1-y[t==0])*(1-t[t==0])))/len(t)
e_all.append(e)
# Print some information.
#print('eta={0}, epoch {1:d}, negative log-likelihood {2:.4f}, w={3}',format(eta, iter, e, w.T))
# Stop iterating if error doesn't change more than tol.
if iter>0:
if numpy.absolute(e-e_all[iter-1]) < tol:
break
all_errors[eta] = e_all
ytest = sps.expit(numpy.dot(Xtest[:],w))
ytest[ytest>0.5] = 1
ytest[ytest<=0.5] = 0
accuracy.append(sum(1 for x,y in zip(ytest,ttest) if x == y) / len(ytest))
print(accuracy)
print(all_errors)
# Plot error over iterations for all etas
plt.figure(10)
plt.rcParams.update({'font.size': 15})
for eta in sorted(all_errors):
plt.plot(all_errors[eta], label='sgd eta={}'.format(eta))
plt.ylabel('Negative log likelihood')
plt.title('Training logistic regression with SGD')
plt.xlabel('Epoch')
plt.axis([0, 25, 0,2])
plt.legend()
plt.show()
# Plot accuracy over etas
plt.figure(10)
plt.rcParams.update({'font.size': 15})
plt.plot(etas,accuracy)
plt.ylabel('Accuracy')
plt.title('Training logistic regression with SGD')
plt.xlabel('Etas')
plt.axis([0, 0.55, 0,1])
plt.legend()
plt.show()
#Dataset non standartized - result not good
#GD
# Error values over all iterations.
all_errors_gd = dict()
accuracy_gd = []
for eta in etas:
# Initialize w.
w = numpy.zeros((23))
w[0] = w[0]+0.1
e_all = []
for iter in range (0, max_iter):
# Compute output using current w on sample x_n.
y = sps.expit(numpy.dot(X[:],w))
# Gradient of the error, using Assignment result
grad_e = numpy.mean(y - t[n])*X[n,:]
# Update w, *subtracting* a step in the error derivative since we're minimizing
w = w - eta*grad_e
# Compute error over all examples, add this error to the end of error vector.
# Compute output using current w on all data X.
y = sps.expit(numpy.dot(X,w))
# e is the error, negative log-likelihood (Eqn 4.90)
e = -numpy.mean(numpy.multiply(t,numpy.log(y)) + numpy.multiply((1-t),numpy.log(1-y)))
#e = -(sum(t[t==1]*numpy.log(y[t==1]))+ sum(numpy.log(1-y[t==0])*(1-t[t==0])))/len(t)
e_all.append(e)
# Print some information.
#print('eta={0}, epoch {1:d}, negative log-likelihood {2:.4f}, w={3}',format(eta, iter, e, w.T))
# Stop iterating if error doesn't change more than tol.
if iter>0:
if numpy.absolute(e-e_all[iter-1]) < tol:
break
all_errors_gd[eta] = e_all
ytest = sps.expit(numpy.dot(Xtest[:],w))
ytest[ytest>0.5] = 1
ytest[ytest<=0.5] = 0
accuracy_gd.append(sum(1 for x,y in zip(ytest,ttest) if x == y) / len(ytest))
print(accuracy_gd)
print(all_errors_gd)
#Question 5
print('\n QUESTION 5')
##dropping draftyear
train = bd[(bd.DraftYear == 2004) | (bd.DraftYear == 2005) |(bd.DraftYear == 2006)]
test = bd[bd.DraftYear == 2007]
train = train.drop(['DraftYear','GP_greater_than_0'],axis=1)
test = test.drop(['DraftYear','GP_greater_than_0'],axis=1)
print('\n\n.... Split into training and testing set + dropping Draftyear\n\n')
# split into input (X) and output (Y) variables
X_train = train.loc[:,train.columns[1:25]]
Y_train = train.loc[:,train.columns[0]]
X_test = test.loc[:,test.columns[1:25]]
Y_test = test.loc[:,test.columns[0]]
X_train = X_train.as_matrix()
Y_train = Y_train.as_matrix()
X_test = X_test.as_matrix()
Y_test = Y_test.as_matrix()
#Models
#Test
#Testing using linear and relu
#Testing with 0, 1 or 2 hidden layer
#Change the number of units on hidden layer
#Optional
#sigmoid on hidden layer (not output)
#learning rates or ADAM
#batch normalization and Dropout
parameters = [
(i,[j1,j2,j3],[k1,k2,k3])
for i in [0,1,2]
for j3 in [6,12,24,48]
for j2 in [6,12,24]
for j1 in [6,12]
for k3 in ['sigmoid','relu','linear']
for k2 in ['sigmoid','relu','linear']
for k1 in ['relu','linear']
]
print(parameters[0:3])
#Functions with 0,1 or 2 layers
#12,24,48 units
#relu or linear
def model(layer,units,act):
# create model
model = Sequential()
if layer==0:
model.add(Dense(1,input_dim=22))
elif layer == 1:
model.add(Dense(units[0],input_dim=22))
model.add(Activation(act[1]))
model.add(Dense(units=1))
elif layer == 2:
model.add(Dense(units[1],input_dim=22))
model.add(Activation(act[1]))
model.add(Dense(units=units[0]))
model.add(Activation(act[2]))
model.add(Dense(units=1))
else:
model.add(Dense(units[2],input_dim=22))
model.add(Activation(act[0]))
model.add(Dense(units=units[1]))
model.add(Activation(act[1]))
model.add(Dense(units=units[0]))
model.add(Activation(act[2]))
model.add(Dense(units=1))
model.add(Activation(act[0]))
model.compile(loss='mean_squared_error', optimizer='adam')
return model
'''
error = []
for ind in range(len(parameters)):
regr = model(parameters[ind][0],parameters[ind][1],parameters[ind][2])
regr.fit(X_train, Y_train, nb_epoch=200, validation_split=0.15, verbose = 50) #batch_size=5,
loss_and_metrics = regr.evaluate(X_test, Y_test, batch_size=256) #batch_size bigger faster
error.append(loss_and_metrics)
if ind % 100 == 0:
parameters2 = pandas.DataFrame(parameters[0:(ind+1)],error)
parameters2.to_csv('a3_tune2.csv')
'''
'''model with dropout'''
def model_drop(layer,units,act):
model = Sequential()
model.add(Dense(units[1],input_dim=22))
model.add(Activation(act[2]))
model.add(Dropout(0.1))
model.add(Dense(units=units[0]))
model.add(Activation(act[1]))
model.add(Dropout(0.1))
model.add(Dense(units=1))
model.add(Activation(act[0]))
model.compile(loss='mean_squared_error', optimizer='adam')
return model
error_drop = []
#1
regr = model_drop(2,[12,6],['linear', 'relu', 'relu'])
regr.fit(X_train, Y_train, nb_epoch=200, validation_split=0.15, verbose = 50) #batch_size=5,
loss_and_metrics = regr.evaluate(X_test, Y_test, batch_size=256) #batch_size bigger faster
error_drop.append(loss_and_metrics)
regr = model_drop(2,[6,6],['relu', 'linear', 'relu'])
regr.fit(X_train, Y_train, nb_epoch=200, validation_split=0.15, verbose = 50) #batch_size=5,
loss_and_metrics = regr.evaluate(X_test, Y_test, batch_size=256) #batch_size bigger faster
error_drop.append(loss_and_metrics)
regr = model_drop(2,[12,24],['linear', 'relu', 'relu'])
regr.fit(X_train, Y_train, nb_epoch=200, validation_split=0.15, verbose = 50) #batch_size=5,
loss_and_metrics = regr.evaluate(X_test, Y_test, batch_size=256) #batch_size bigger faster
error_drop.append(loss_and_metrics)
regr = model_drop(2,[12,12],['linear', 'linear', 'relu'])
regr.fit(X_train, Y_train, nb_epoch=200, validation_split=0.15, verbose = 50) #batch_size=5,
loss_and_metrics = regr.evaluate(X_test, Y_test, batch_size=256) #batch_size bigger faster
error_drop.append(loss_and_metrics)
regr = model_drop(2,[12,6],['linear', 'linear', 'relu'])
regr.fit(X_train, Y_train, nb_epoch=200, validation_split=0.15, verbose = 50) #batch_size=5,
loss_and_metrics = regr.evaluate(X_test, Y_test, batch_size=256) #batch_size bigger faster
error_drop.append(loss_and_metrics)
print("Top models testing error - dropout",error_drop)
error = []
#1
regr = model(2,[12,6],['linear', 'relu', 'relu'])
regr.fit(X_train, Y_train, nb_epoch=200, validation_split=0.15, verbose = 50) #batch_size=5,
loss_and_metrics = regr.evaluate(X_test, Y_test, batch_size=256) #batch_size bigger faster
error.append(loss_and_metrics)
regr = model(2,[6,6],['relu', 'linear', 'relu'])
regr.fit(X_train, Y_train, nb_epoch=200, validation_split=0.15, verbose = 50) #batch_size=5,
loss_and_metrics = regr.evaluate(X_test, Y_test, batch_size=256) #batch_size bigger faster
error.append(loss_and_metrics)
regr = model(2,[12,24],['linear', 'relu', 'relu'])
regr.fit(X_train, Y_train, nb_epoch=200, validation_split=0.15, verbose = 50) #batch_size=5,
loss_and_metrics = regr.evaluate(X_test, Y_test, batch_size=256) #batch_size bigger faster
error.append(loss_and_metrics)
regr = model(2,[12,12],['linear', 'linear', 'relu'])
regr.fit(X_train, Y_train, nb_epoch=200, validation_split=0.15, verbose = 50) #batch_size=5,
loss_and_metrics = regr.evaluate(X_test, Y_test, batch_size=256) #batch_size bigger faster
error.append(loss_and_metrics)
regr = model(2,[12,6],['linear', 'linear', 'relu'])
regr.fit(X_train, Y_train, nb_epoch=200, validation_split=0.15, verbose = 50) #batch_size=5,
loss_and_metrics = regr.evaluate(X_test, Y_test, batch_size=256) #batch_size bigger faster
error.append(loss_and_metrics)
print("Top models testing error",error)
#exec(open("assignment3_raquelaoki.py").read())