-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
376 lines (299 loc) · 11.8 KB
/
test.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
def readMockData():
import json
f = open('testdata.txt', 'r')
data = []
while 1:
line = f.readline()
if not line:
break
data.append(json.loads(line))
pass
return data
data = readMockData()
import numpy
import scipy
if __name__ == '__main__':
print(numpy.__version__)
print(scipy.__version__)
from hw1_lr import LinearRegression, LinearRegressionWithL2Loss
from utils import mean_squared_error
import matplotlib.pyplot as plt
from data import generate_data_part_1
features, values = generate_data_part_1()
features = data[0]
values = data[1]
model = LinearRegression(nb_features=1)
model.train(features, values)
mse = mean_squared_error(values, model.predict(features))
print('[part 1.2]\tmse: {mse:.5f}'.format(mse=mse))
plt.scatter([x[0] for x in features], values, label='origin');
plt.plot([x[0] for x in features], model.predict(features), label='predicted');
plt.legend()
from data import generate_data_part_2
features, values = generate_data_part_2()
features = data[2]
values = data[3]
model = LinearRegression(nb_features=1)
model.train(features, values)
mse = mean_squared_error(values, model.predict(features))
print('[part 1.3.1]\tmse: {mse:.5f}'.format(mse=mse))
plt.scatter([x[0] for x in features], values, label='origin');
plt.plot([x[0] for x in features], model.predict(features), label='predicted');
plt.legend()
from utils import polynomial_features
features, values = generate_data_part_2()
features = data[2]
values = data[3]
plt.scatter([x[0] for x in features], values, label='origin');
for k in [2, 4, 8]:
features_extended = polynomial_features(features, k)
#print(features_extended)
model = LinearRegression(nb_features=k)
model.train(features_extended, values)
mse = mean_squared_error(values, model.predict(features_extended))
print('[part 1.3.2]\tk: {k:d}\tmse: {mse:.5f}'.format(k=k, mse=mse))
plt.plot([x[0] for x in features], model.predict(features_extended), label='k={k}'.format(k=k));
plt.legend()
from data import generate_data_part_3
features, values = generate_data_part_3()
features = data[4]
values = data[5]
train_features, train_values = features[:100], values[:100]
valid_features, valid_values = features[100:120], values[100:120]
test_features, test_values = features[120:], values[120:]
assert len(train_features) == len(train_values) == 100
assert len(valid_features) == len(valid_values) == 20
assert len(test_features) == len(test_values) == 30
best_mse, best_k = 1e10, -1
for k in [1, 3, 10]:
train_features_extended = polynomial_features(train_features, k)
model = LinearRegression(nb_features=k)
model.train(train_features_extended, train_values)
train_mse = mean_squared_error(train_values, model.predict(train_features_extended))
valid_features_extended = polynomial_features(valid_features, k)
valid_mse = mean_squared_error(valid_values, model.predict(valid_features_extended))
print('[part 1.4.1]\tk: {k:d}\t'.format(k=k) +
'train mse: {train_mse:.5f}\tvalid mse: {valid_mse:.5f}'.format(
train_mse=train_mse, valid_mse=valid_mse))
if valid_mse < best_mse:
best_mse, best_k = valid_mse, k
combined_features_extended = polynomial_features(train_features + test_features, best_k)
model = LinearRegression(nb_features=best_k)
model.train(combined_features_extended, train_values + test_values)
test_features_extended = polynomial_features(test_features, best_k)
test_mse = mean_squared_error(test_values, model.predict(test_features_extended))
print('[part 1.4.1 Linear Regression]\tbest_k: {best_k:d}\ttest mse: {test_mse:.5f}'.format(
best_k=best_k, test_mse=test_mse))
from itertools import product
best_mse, best_k, best_alpha = 1e10, -1, -1
for k, alpha in product([1, 3, 10], [0.01, 0.1, 1, 10]):
train_features_extended = polynomial_features(train_features, k)
model = LinearRegressionWithL2Loss(nb_features=k, alpha=alpha)
model.train(train_features_extended, train_values)
train_mse = mean_squared_error(train_values, model.predict(train_features_extended))
valid_features_extended = polynomial_features(valid_features, k)
valid_mse = mean_squared_error(valid_values, model.predict(valid_features_extended))
print('[part 1.4.2]\tk: {k:d}\talpha: {alpha}\t'.format(k=k, alpha=alpha) +
'train mse: {train_mse:.5f}\tvalid mse: {valid_mse:.5f}'.format(
train_mse=train_mse, valid_mse=valid_mse))
if valid_mse < best_mse:
best_mse, best_k, best_alpha = valid_mse, k, alpha
combined_features_extended = polynomial_features(train_features + test_features, best_k)
model = LinearRegressionWithL2Loss(nb_features=best_k, alpha=best_alpha)
model.train(combined_features_extended, train_values + test_values)
test_features_extended = polynomial_features(test_features, best_k)
test_mse = mean_squared_error(test_values, model.predict(test_features_extended))
print('[part 1.4.2]\tbest_k: {best_k:d}\tbest_alpha: {best_alpha:f}\t'.format(
best_k=best_k, best_alpha=best_alpha) +
'test mse: {test_mse:.5f}'.format(test_mse=test_mse))
from hw1_knn import KNN
from utils import euclidean_distance, gaussian_kernel_distance, inner_product_distance
from utils import f1_score
distance_funcs = {
'euclidean': euclidean_distance,
'gaussian': gaussian_kernel_distance,
'inner_prod': inner_product_distance,
}
from data import generate_data_cancer
features, labels = generate_data_cancer()
features = data[6]
labels = data[7]
train_features, train_labels = features[:400], labels[:400]
valid_features, valid_labels = features[400:460], labels[400:460]
test_features, test_labels = features[460:], labels[460:]
assert len(train_features) == len(train_labels) == 400
assert len(valid_features) == len(valid_labels) == 60
assert len(test_features) == len(test_labels) == 109
for name, func in distance_funcs.items():
best_f1_score, best_k = -1, 0
for k in [1, 3, 10, 20, 50]:
model = KNN(k=k, distance_function=func)
model.train(train_features, train_labels)
train_f1_score = f1_score(
train_labels, model.predict(train_features))
valid_f1_score = f1_score(
valid_labels, model.predict(valid_features))
print('[part 2.1] {name}\tk: {k:d}\t'.format(name=name, k=k) +
'train: {train_f1_score:.5f}\t'.format(train_f1_score=train_f1_score) +
'valid: {valid_f1_score:.5f}'.format(valid_f1_score=valid_f1_score))
if valid_f1_score > best_f1_score:
best_f1_score, best_k = valid_f1_score, k
model = KNN(k=best_k, distance_function=func)
model.train(train_features + valid_features,
train_labels + valid_labels)
test_f1_score = f1_score(test_labels, model.predict(test_features))
print()
print('[part 2.1] {name}\tbest_k: {best_k:d}\t'.format(name=name, best_k=best_k) +
'test f1 score: {test_f1_score:.5f}'.format(test_f1_score=test_f1_score))
print()
from utils import NormalizationScaler, MinMaxScaler
scaling_functions = {
'min_max_scale': MinMaxScaler,
'normalize': NormalizationScaler,
}
for scaling_name, scaling_class in scaling_functions.items():
for name, func in distance_funcs.items():
scaler = scaling_class()
train_features_scaled = scaler(train_features)
valid_features_scaled = scaler(valid_features)
best_f1_score, best_k = 0, -1
for k in [1, 3, 10, 20, 50]:
model = KNN(k=k, distance_function=func)
model.train(train_features_scaled, train_labels)
train_f1_score = f1_score(
train_labels, model.predict(train_features_scaled))
valid_f1_score = f1_score(
valid_labels, model.predict(valid_features_scaled))
print(
'[part 2.2] {name}\t{scaling_name}\tk: {k:d}\t'.format(name=name, scaling_name=scaling_name, k=k) +
'train: {train_f1_score:.5f}\t'.format(train_f1_score=train_f1_score) +
'valid: {valid_f1_score:.5f}'.format(valid_f1_score=valid_f1_score))
if valid_f1_score > best_f1_score:
best_f1_score, best_k = valid_f1_score, k
# now change it to new scaler, since the training set changes
scaler = scaling_class()
combined_features_scaled = scaler(train_features + valid_features)
test_features_scaled = scaler(test_features)
model = KNN(k=best_k, distance_function=func)
model.train(combined_features_scaled, train_labels + valid_labels)
test_f1_score = f1_score(test_labels, model.predict(test_features_scaled))
print()
print('[part 2.2] {name}\t{scaling_name}\t'.format(name=name, scaling_name=scaling_name) +
'best_k: {best_k:d}\ttest: {test_f1_score:.5f}'.format(best_k=best_k, test_f1_score=test_f1_score))
print()
import matplotlib.pyplot as plt
from data import generate_data_perceptron
from hw1_perceptron import Perceptron
import numpy as np
## To clearly visualize the problem, we just use 2 features for now
## y = f(x1,x2)
nb_features=2
model = Perceptron(nb_features=nb_features)
x, y = generate_data_perceptron(nb_features=nb_features, seperation=2)
x = data[8]
y = data[9]
x1 = []
x2 = []
for i in x:
x1.append(i[1])
x2.append(i[2])
plt.scatter(x1, x2, c=y)
##plt.show()
converged = model.train(x, y)
y_hat = model.predict(x)
correct = 0
for i, y_real in enumerate(y):
if (y_hat[i] == y_real):
correct = correct + 1
print("Accuracy on training data is {}".format(correct * 100 / len(y)))
print(correct)
w = model.get_weights()
x1 = []
x2 = []
for i in x:
x1.append(i[1])
x2.append(i[2])
plt.scatter(x1, x2, c=y)
plt.plot(np.arange(-3, 3, 1), -(w[1] * np.arange(-3, 3, 1) + w[0]) / w[2])
##plt.show()
model.reset()
print(model.get_weights())
test_x, train_x = x[80:], x[:80]
test_y, train_y = y[80:], y[:80]
converged = model.train(train_x, train_y)
y_hat = model.predict(test_x)
correct = 0
for i, y_real in enumerate(test_y):
if (y_hat[i] == y_real):
correct = correct + 1
print("Accuracy on testing data is {}".format(correct * 100 / len(test_y)))
w = model.get_weights()
x1 = []
x2 = []
for i in x:
x1.append(i[1])
x2.append(i[2])
plt.scatter(x1, x2, c=y)
plt.plot(np.arange(-3, 3, 1), -(w[1] * np.arange(-3, 3, 1) + w[0]) / w[2])
#plt.show()
nb_features=2
model = Perceptron(nb_features=nb_features)
x, y = generate_data_perceptron(nb_features=nb_features, seperation=1)
# plot and satisfy your self that data is not linearly seperable
x = data[10]
y = data[11]
x1 = []
x2 = []
for i in x:
x1.append(i[1])
x2.append(i[2])
plt.scatter(x1, x2, c=y)
#plt.show()
converged = model.train(x, y)
if (converged):
print('Algorithm has converged')
else:
print('Algorithm didnot converge')
y_hat = model.predict(x)
correct = 0
for i, y_real in enumerate(y):
if (y_hat[i] == y_real):
correct = correct + 1
print("Accuracy on training data is {}".format(correct * 100 / len(y)))
print(correct)
w = model.get_weights()
x1 = []
x2 = []
for i in x:
x1.append(i[1])
x2.append(i[2])
plt.scatter(x1, x2, c=y)
plt.plot(np.arange(-3, 3, 1), -(w[1] * np.arange(-3, 3, 1) + w[0]) / w[2])
#plt.show()
nb_features=10
model = Perceptron(nb_features=nb_features)
# use seperation=1 for non-seperable
# use seperation=2 for seperable
x, y = generate_data_perceptron(nb_features=nb_features, seperation=1)
# plot first two dimensions
x = data[12]
y = data[13]
x1 = []
x2 = []
for i in x:
x1.append(i[1])
x2.append(i[2])
plt.scatter(x1, x2, c=y)
#plt.show()
converged = model.train(x, y)
if (converged):
print('Algorithm has converged')
else:
print('Algorithm didnot converge')
y_hat = model.predict(x)
correct = 0
for i, y_real in enumerate(y):
if (y_hat[i] == y_real):
correct = correct + 1
print("Accuracy on training data is {}".format(correct * 100 / len(y)))
print(correct)