-
Notifications
You must be signed in to change notification settings - Fork 1
/
task2.py
65 lines (56 loc) · 2.42 KB
/
task2.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
import numpy as np
from numpy.linalg import inv
import matplotlib.pyplot as plt
import random
def calculate_w(l, phi, t, N_rows, M_cols):
LI = np.eye(M_cols) * l
innerPrdt = (LI + np.matmul(np.transpose(phi),phi))
w = np.matmul(inv(innerPrdt),np.matmul(np.transpose(phi),t))
return w
def avg(lst):
return sum(lst)/len(lst)
def learning_curves(train, trainR, test, testR, dataset_name):
l1 = 5
l2 = 27
l3 = 145
mse_lambda1 = []
mse_lambda2 = []
mse_lambda3 = []
N_rows_test = np.shape(test)[0]
M_features_train = np.shape(train)[1]
#training_set_sizes = [10,50,100,150,200,250,300,350,400,450,500,550,600,650,700,750,800,850,900,950,1000]
training_set_sizes = [x for x in range(10,1001,15)]
for t in training_set_sizes:
N_rows_train = t
w1,w2,w3 = [],[],[]
for trial in range(0,10):
train_sub, trainR_sub = zip(*random.sample(list(zip(train, trainR)), t))
w1.append(calculate_w(l1, train_sub, trainR_sub, N_rows_train, M_features_train))
w2.append(calculate_w(l2, train_sub, trainR_sub, N_rows_train, M_features_train))
w3.append(calculate_w(l3, train_sub, trainR_sub, N_rows_train, M_features_train))
mse_lambda1.append(sum((np.matmul(test,avg(w1)) - testR) ** 2) / N_rows_test)
mse_lambda2.append(sum((np.matmul(test,avg(w2)) - testR) ** 2) / N_rows_test)
mse_lambda3.append(sum((np.matmul(test,avg(w3)) - testR) ** 2) / N_rows_test)
font = {'family' : 'normal',
'weight' : 'bold',
'size' : 22}
plt.rc('font', **font)
#plt.subplot(3, 1, 1)
plt.plot(training_set_sizes,mse_lambda1, label = "MSE(Lambda = "+str(l1)+")")
plt.xlabel('Training Set Sizes',fontsize=30)
plt.ylabel('MSE',fontsize=30)
plt.title('Learning Curves, Dataset: '+dataset_name,fontsize=40)
plt.legend(fontsize=30)
plt.show()
#plt.subplot(3, 1, 2)
plt.plot(training_set_sizes,mse_lambda2, label = "MSE(Lambda = "+str(l2)+")")
plt.xlabel('Training Set Sizes',fontsize=30)
plt.ylabel('MSE',fontsize=30)
plt.legend(fontsize=30)
plt.show()
#plt.subplot(3, 1, 3)
plt.plot(training_set_sizes,mse_lambda3, label = "MSE(Lambda = "+str(l3)+")")
plt.xlabel('Training Set Sizes',fontsize=30)
plt.ylabel('MSE',fontsize=30)
plt.legend(fontsize=30)
plt.show()