-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex5.py
155 lines (113 loc) · 5.36 KB
/
ex5.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
import matplotlib.pyplot as plt
import numpy as np
import scipy.io as sio
import scipy.optimize as op
from sklearn.preprocessing import PolynomialFeatures
import ml.feature as feature
import ml.learning_curves as lc
import ml.linear_regression as lire
data = sio.loadmat("ml_course_material/machine-learning-ex5/ex5/ex5data1.mat")
X_train = data["X"]
y_train = data["y"]
X_cv = data["Xval"]
y_cv = data["yval"]
X_test = data["Xtest"]
y_test = data["ytest"]
# plt.plot(X_train, y_train, "rx")
# plt.xlabel("Change in water level")
# plt.ylabel("Water flowing out of the dam")
# plt.show()
cost = lire.linear_regression_cost(np.hstack((np.ones((len(X_train), 1)), X_train)), y_train, np.array([[1], [1]]), 1)
print(f"cost={cost}")
gradient = lire.linear_regression_cost_derivative(np.hstack((np.ones((len(X_train), 1)), X_train)), y_train,
np.array([[1], [1]]), 1)
print(gradient)
initial_theta = np.zeros((X_train.shape[1] + 1, 1))
X_train_with_bias = np.hstack((np.ones((len(X_train), 1)), X_train))
result = op.minimize(fun=lire.linear_regression_cost_gradient,
x0=initial_theta.reshape((-1)),
args=(X_train_with_bias, y_train),
method="CG",
jac=True,
options={"maxiter": 400})
print(result)
x_points = list(range(-50, 50))
y_points = [result.x @ np.array([[1], [x_point]]) for x_point in x_points]
plt.figure(0)
plt.plot(X_train, y_train, "rx")
plt.plot(x_points, y_points)
plt.xlabel("Change in water level")
plt.ylabel("Water flowing out of the dam")
plt.show()
def minimize(X, y, regularization_lambda=0):
initial_theta = np.zeros((X.shape[1], 1))
result = op.minimize(fun=lire.linear_regression_cost_gradient,
x0=initial_theta.reshape((-1)),
args=(X, y, regularization_lambda),
method="CG",
jac=True,
options={"maxiter": 400})
return result.x.reshape((-1, 1))
def cost_lire(X, y, theta):
return lire.linear_regression_cost(X, y, theta)[0][0]
def with_bias(X):
return np.hstack((np.ones((X.shape[0], 1)), X))
class LinearRegressionEstimatorPredictor:
def __init__(self, regularization_lambda=0):
self.regularization_lambda = regularization_lambda
def fit(self, X, y):
self.theta = minimize(X, y, self.regularization_lambda)
def predict(self, X):
return X @ self.theta
def cost(y, y_pred):
m = len(y)
diff = y_pred - y
return ((diff.T @ diff) / (2 * m))[0][0]
plt.figure(1)
(j_train, j_cv) = lc.learning_curves_of_different_training_set_size(X_train, y_train, X_cv, y_cv,
LinearRegressionEstimatorPredictor(), cost)
plt.figure(2)
(j_train, j_cv) = lc.learning_curves_of_different_polynomial_degree(X_train, y_train, X_cv, y_cv,
LinearRegressionEstimatorPredictor(), cost, 12)
poly_features = PolynomialFeatures(8, include_bias=False)
X_train_poly = poly_features.fit_transform(X_train)
normalizer = feature.FeatureNormalizer(X_train_poly)
X_train_poly = normalizer.normalize_matrix(X_train_poly)
X_cv_poly = poly_features.fit_transform(X_cv)
X_cv_poly = normalizer.normalize_matrix(X_cv_poly)
X_test_poly = poly_features.fit_transform(X_test)
X_test_poly = normalizer.normalize_matrix(X_test_poly)
regularization_lambda = 1
result = op.minimize(fun=lire.linear_regression_cost_gradient,
x0=np.zeros(X_train_poly.shape[1] + 1),
args=(
np.hstack((np.ones((X_train_poly.shape[0], 1)), X_train_poly)), y_train,
regularization_lambda),
method="CG",
jac=True,
options={"maxiter": 400})
x_points = np.linspace(-60, 70, 1000).reshape((-1, 1))
X_poly = poly_features.fit_transform(x_points)
X_poly = normalizer.normalize_matrix(X_poly)
X_poly = np.hstack((np.ones((X_poly.shape[0], 1)), X_poly))
plt.figure(3)
plt.plot(X_train, y_train, "rx")
plt.plot(x_points, X_poly @ result.x)
plt.show()
plt.figure(4)
(j_train, j_cv) = lc.learning_curves_of_different_training_set_size(X_train_poly, y_train, X_cv_poly, y_cv,
LinearRegressionEstimatorPredictor(
regularization_lambda),
cost)
plt.figure(5)
(j_train, j_cv, regularization_lambdas) = lc.learning_curves_of_different_lambda(X_train_poly, y_train, X_cv_poly, y_cv,
LinearRegressionEstimatorPredictor,
cost)
# theta = minimize(with_bias(X_train_poly), y_train, 3)
#
# print(f"cv error: {cost(with_bias(X_cv_poly), y_cv, theta)}")
# print(f"test error: {cost(with_bias(X_test_poly), y_test, theta)}")
(j_train_means, j_cv_means) = lc.learning_curves_on_random_sets(X_train_poly, y_train, X_cv_poly, y_cv,
LinearRegressionEstimatorPredictor(0.01),
cost,)
plt.figure(6)