-
Notifications
You must be signed in to change notification settings - Fork 0
/
lr.py
315 lines (162 loc) · 8.44 KB
/
lr.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
#!/usr/bin/env python
# coding: utf-8
# $$h_{\theta}(x^{i}) = \theta_{0} x_{0}^i+\theta_{1} x_{1}^i+\theta_{2} x_{2}^i+\theta_{j} x_{j}^i \tag{1}$$
# $$J(\theta) = \frac{1}{2m}\sum\limits_{i=1}^{m}(h_{\theta}(x^{i})-y^{i})^2 \tag{2}$$
# $$J(\theta) = \frac{1}{2m}\sum\limits_{i=1}^{m}((\theta_{0} x_{0}^i+\theta_{1} x_{1}^i+\theta_{2} x_{2}^i+\theta_{j} x_{j}^i)-y^{i})^2 \tag{3}$$
# $$\frac{\partial J(\theta)}{\partial \theta_{j}} = \frac{1}{m}\sum\limits_{i=1}^{m}(h_{\theta}(x^{i})-y^{i})x_{j}^i \tag{4}$$
# $$\frac{\partial J(\theta)}{\partial \theta_{j}} = \frac{1}{m}\sum\limits_{i=1}^{m}((\theta_{0} x_{0}^i+\theta_{1} x_{1}^i+\theta_{2} x_{2}^i+\theta_{j} x_{j}^i)-y^{i})x_{j}^i \tag{5}$$
# $$\theta_{j} = \theta_{j} - \alpha \frac{1}{m}\sum\limits_{i=1}^{m}((\theta_{0} x_{0}^i+\theta_{1} x_{1}^i+\theta_{2} x_{2}^i+\theta_{j} x_{j}^i)-y^{i})x_{j}^i \tag{6}$$
# $$J(\theta) = \frac{1}{2m}(x \theta - y)^T(x \theta - y) \tag{7}$$
# $$\frac{\partial J(\theta)}{\partial \theta} = \frac{1}{m}x^T(x \theta - y) \tag{8}$$
# In[ ]:
import numpy as np
import random
random.seed(63)
import matplotlib.pyplot as plt
# In[ ]:
#generate random data with seed for reproducibility
np.random.seed(63)
a = np.random.randn(1000,10)
# In[ ]:
np.random.seed(63)
b = np.random.randn(1000,1)
# In[ ]:
class lr(object):
"""
Class constructor.
"""
def __init__(self,x0:np.ndarray, y0:np.ndarray,lr:float=0.1,n_iters:int=100):
"""
Constructor method.
"""
self.x0 = x0
self.y0 = y0
assert(type(x0)==np.ndarray and type(y0)==np.ndarray)
self.lr = lr
assert type(lr)==float
self.n_iters = n_iters
self.m, self.n = np.shape(self.x0)[0], np.shape(self.x0)[1]
# 1D array of size n_features
#initialise theta
#column vector of of shape(10,1)
#np.ones creates a tuple (10,)
self.th = np.ones(self.n).reshape(-1,1)
def cost_not_vectorised(self, theta):
#this indicates that cost will be a float
#calculate cost at each row or sample and then sum
c = 0
for i in range(self.m):
#y_hat prediction for each row with all estimated parameters
#at each row, iterate over all columns/parameters stored in theta
y_hat = 0
for j in range(self.n):
#element-wise multiplication
#not matrix multiplication
#self.x0 of shape(1000,10) and theta of shape (10,1)
#equation 1
y_hat += theta[j]*self.x0[i][j]
#np.power because of neg distance and punishing large outliers
#cost for row i, substract actual from prediction
#equation 2 (short version) or equation 3 (long version)
c_i = (y_hat - self.y0[i])**2
c += c_i
cost = (1/(2*self.m))*float(c)
return cost
def gradient_descent_not_vectorised(self):
cost_list = []
for _ in range(self.n_iters):
#here starts equation 4 (short version) or equation 5 (long version)
#first iterate over num of features
#calc y_hat - iterate over samples and features as in previous step
#calculate derivative at that row i (sample i)
#sum derivatives from all rows for a single theta j
#repeat over all j
derivatives_list = []
for k in range(self.n):
#sum derivatives from all rows
d_j = 0
for i in range(self.m):
#estimate
y_hat = 0
for j in range(self.n):
y_hat += self.th[j]*self.x0[i][j]
#derivative for each row
#cannot be indexed by j because out of that for loop at this step
#index by k because derivative for k parameter
#derivative of theta j at row i
d_i = (y_hat - self.y0[i])*self.x0[i][k]
#sum all rows and store as d_j, derivative of theta j (sum all rows i)
d_j += d_i
#append derivative for each column (of each parameter) to a list
derivatives = (1/self.m)*d_j
derivatives_list.append(derivatives)
#update params stored as self.th according to lr rate
#both self.th and derivatives_list are column vectors of shape (10,1)
#indexed by z, hence result is a column vector of original shape
derivatives_list = np.array(derivatives_list).reshape(-1,1)
for z in range(len(self.th)):
#equation 6
self.th[z] = self.th[z] - self.lr*derivatives_list[z]
#pass updated parameters to the cost function
cost = self.cost_not_vectorised(self.th)
#append to list and start iterate again
cost_list.append(cost)
plt.plot(cost_list)
return cost_list
def cost_vectorised(self, theta):
#b refers to x@theta - y in equation 7 for conciseness
#self.x0 of shape (1000,10) and self.th of shape (10,1)
#the matrix multiplication result of shape (1000,1)
if np.shape(self.x0)[1] == np.shape(theta)[0]:
a = self.x0@theta
a = a.reshape(-1,1) #shape (1000,1), not necessary
#vector shape (1000,1) necessary for column vector substraction in vectorised form
if np.shape(a) == (np.size(self.x0,0),np.size(theta,1)):
#all three column vectors of shape (1000,1)
b = a - self.y0
if np.shape(b.T)[1] == np.shape(b)[0]:
#b.T of shape (1,1000) and b of shape (1000,1)
#matrix multiplication result of shape (1,1), which is a scalar
#equation 7
c = b.T@b
cost = (1/(2*self.m))*float(c)
else:
#if this not true, matrix multiplication cannot be calculated
print(f"{np.shape(b.T)[1]} must be equal to {np.shape(b)[0]}")
else:
#check if a = x theta is of correct shape
print(f"vector a must be of size ({np.size(self.x0,0)},{np.size(theta,1)})")
else:
#if this not true, matrix multiplication cannot be calculated
print(f"{np.shape(self.x0)[1]} not equal to {np.shape(theta)[0]}")
return cost
def gradient_descent_vectorised(self):
cost_list1 = []
for z in range(self.n_iters):
#shape(1000,1)
a = np.reshape(self.x0@self.th - self.y0,(-1,1))
#shape (10,1000)
b = self.x0.T
#matrix multiplication result of shape (10,1) which is exactly shape of self.th
#c is the derivative of the cost function w.r.t. j, equation 8
c = b@a
c = (1/self.m)*c
#old parameter value minus (learning rate x derivative)
#update params according to learning rate and derivative calculated above
self.th = self.th - self.lr*c
cost_i = self.cost_vectorised(self.th)
cost_list1.append(cost_i)
plt.plot(cost_list1)
return cost_list1
# In[ ]:
a = lr(a,b,0.1,100)
# In[ ]:
#a.cost_not_vectorised(a.th)
# In[ ]:
#a.gradient_descent_not_vectorised()
# In[ ]:
a.cost_vectorised(a.th)
# In[ ]:
a.gradient_descent_vectorised()
# In[ ]:
# In[ ]: