-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimplementations.py
196 lines (160 loc) · 8.42 KB
/
implementations.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
"""Some Machine Learning Methods done during CS-433."""
# Useful starting lines
import pickle
import datetime
import numpy as np
"""Functions"""
def check_input(y, tx, lambda_ = 0, initial_w = np.array([0,0]), max_iters = 0, gamma = 0):
"""check that all types are correct takes more time"""
y_check = y.astype(float)
tx_check = tx.astype(float)
lambda__check = float(lambda_)
w_check = initial_w.astype(float)
max_iters_check = int(max_iters)
gamma_check = float(gamma)
return y_check, tx_check, lambda__check, w_check, max_iters_check, gamma_check
def compute_loss_MSE(y, tx, w):
"""calculate loss using mean squared error"""
e = y - tx @ w
loss = 1/(2*np.shape(tx)[0]) * e.T @ e
return loss
def compute_loss_logistic_regression(y, tx, w):
"""calculate loss for logistic regression"""
sigmoid = 1 / (1 + np.exp(-(tx @ w)))
loss = -1 / np.shape(tx)[0] * np.sum((1 - y) * np.log(1 - sigmoid) + y * np.log(sigmoid))
return loss
def shuffle_dataset(y, tx):
"""shuffling dataset"""
# np.random.seed(1) #if commented selects every time you run a different seed
random_shuffle = np.random.permutation(np.arange(np.shape(tx)[0]))
shuffled_y = y[random_shuffle]
shuffled_tx = tx[random_shuffle]
return shuffled_y, shuffled_tx
def least_squares_GD(y, tx, initial_w, max_iters, gamma):
"""Computes least squares using Gradient Descent"""
y, tx, lambda_, w, max_iters, gamma = check_input(y, tx, initial_w = initial_w,
max_iters = max_iters, gamma = gamma)
for n_iter in range(max_iters):
e = y - tx @ w
gradient = -1 / np.shape(tx)[0] * tx.T @ e
w = w - gamma * gradient
loss = compute_loss_MSE(y, tx, w)
return w, loss
def least_squares_SGD(y, tx, initial_w, max_iters, gamma):
"""Computes least squares using Stochastic Gradient Descent"""
y, tx, lambda_, w, max_iters, gamma = check_input(y, tx, initial_w=initial_w,
max_iters=max_iters, gamma=gamma)
shuffled_y, shuffled_tx = shuffle_dataset(y, tx)
for n_iter in range(max_iters):
for training_example in range(np.shape(tx)[0]):
e = shuffled_y[training_example] -shuffled_tx[training_example] @ w
gradient = -e * shuffled_tx[training_example]
w = w - gamma * gradient
loss = compute_loss_MSE(shuffled_y, shuffled_tx, w)
return w, loss
def least_squares(y, tx):
"""Computes least squares using Normal equations"""
y, tx, lambda_, w, max_iters, gamma = check_input(y, tx)
w = np.linalg.inv(tx.T @ tx) @ tx.T @ y
loss = compute_loss_MSE(y, tx, w)
return w, loss
def ridge_regression(y, tx, lambda_ ):
"""Computes ridge regression using normal equations"""
y, tx, lambda_, w, max_iters, gamma = check_input(y, tx, lambda_=lambda_)
w = np.linalg.inv(tx.T @ tx + lambda_ * 2 * np.shape(y)[0] * np.eye(np.shape(tx)[1])) @ tx.T @ y
loss = compute_loss_MSE(y, tx, w) + lambda_ * w.T @ w
return w, loss
def logistic_regression(y, tx, initial_w, max_iters, gamma):
"""Computes logistic regression using gradient descent"""
y, tx, lambda_, w, max_iters, gamma = check_input(y, tx, initial_w=initial_w,
max_iters=max_iters, gamma=gamma)
for n_iter in range(max_iters):
sigmoid = 1/ (1 + np.exp(-(tx @ w)))
gradient = -1/np.shape(tx)[0] * tx.T @ (y-sigmoid)
w = w-gamma * gradient
loss = compute_loss_logistic_regression(y, tx, w)
return w, loss
def reg_logistic_regression(y, tx, lambda_ , initial_w, max_iters, gamma):
"""Computes regularized logistic regression using gradient descent"""
y, tx, lambda_, w, max_iters, gamma = check_input(y, tx, lambda_ = lambda_,initial_w=initial_w,
max_iters=max_iters, gamma=gamma)
for n_iter in range(max_iters):
sigmoid = 1 / (1 + np.exp(-(tx @ w)))
gradient = -1 / np.shape(tx)[0] * tx.T @ (y - sigmoid) + 2 * lambda_ * w
w = w - gamma * gradient
loss = compute_loss_logistic_regression(y, tx, w) + lambda_ * w.T @ w
return w, loss
"""Testing"""
if __name__ == "__main__":
loop = True
while loop:
input_user = input('Test:\n 1 Least square \n 2 Logistic regression \n 3 end \n ')
if int(input_user) == 1:
"""load data for least squares"""
with open(r"test_ML_methods/data.pickle", "rb") as input_file:
data = pickle.load(input_file)
y = data[0]
tx = data[1]
test = input('ML Methods\n 1 least_squares_GD \n 2 least_squares_SGD \n 3 least_squares(Normal Equation) \n'
' 4 ridge_regression(Normal Equation) \n ')
if int(test) == 1:
"""run least_squares_GD"""
start_time = datetime.datetime.now()
function = least_squares_GD(y, tx, np.array([0, 0]), 14, 0.7)
print('weights = ' , function[0], 'loss = ', function[1])
end_time = datetime.datetime.now()
exection_time = (end_time - start_time).total_seconds()
print("Gradient Descent: execution time={t:.3f} seconds".format(t=exection_time))
elif int(test) == 2:
"""run least_squares_SGD"""
start_time = datetime.datetime.now()
function = least_squares_SGD(y, tx, np.array([0, 0]), 1, 0.01)
print('weights = ', function[0], 'loss = ', function[1])
end_time = datetime.datetime.now()
exection_time = (end_time - start_time).total_seconds()
print("Stochastic Gradient Descent: execution time={t:.3f} seconds".format(t=exection_time))
elif int(test) == 3:
"""run least_squares(Normal Equation)"""
start_time = datetime.datetime.now()
function = least_squares(y, tx)
print('weights = ', function[0], 'loss = ', function[1])
end_time = datetime.datetime.now()
exection_time = (end_time - start_time).total_seconds()
print("Normal equation least squares: execution time={t:.3f} seconds".format(t=exection_time))
elif int(test) == 4:
"""run ridge_regression(Normal Equation)"""
start_time = datetime.datetime.now()
function = ridge_regression(y, tx, 0.0001)
print('weights = ', function[0], 'loss = ', function[1])
end_time = datetime.datetime.now()
exection_time = (end_time - start_time).total_seconds()
print("Regularized Normal equation least squares: execution time={t:.3f} seconds".format(t=exection_time))
else:
loop = False
elif int(input_user) == 2:
"""load data for logistic regression"""
with open(r"test_ML_methods/tx_regression.pickle", "rb") as input_file:
tx = pickle.load(input_file)
with open(r"test_ML_methods/y_regression.pickle", "rb") as input_file:
y = pickle.load(input_file)
test = input('ML Methods\n 1 logistic_regression(Gradient Descent) \n 2 reg_logistic_regression(Gradient Descent) \n ')
if int(test) == 1:
"""run logistic_regression"""
start_time = datetime.datetime.now()
function = logistic_regression(y, tx, np.zeros((tx.shape[1], 1)), 1846, 1)
print('weights = ', function[0], 'loss = ', function[1])
end_time = datetime.datetime.now()
exection_time = (end_time - start_time).total_seconds()
print("logistic regression: execution time={t:.3f} seconds".format(t=exection_time))
elif int(test) == 2:
"""run ridge_regression(Normal Equation)"""
start_time = datetime.datetime.now()
function = reg_logistic_regression(y, tx, 0, np.zeros((tx.shape[1], 1)), 1846, 1)
print('weights = ', function[0], 'loss = ', function[1])
end_time = datetime.datetime.now()
exection_time = (end_time - start_time).total_seconds()
print("regularized logistic regression: execution time={t:.3f} seconds".format(t=exection_time))
elif int(input_user) ==3:
loop = False
else:
loop = False