-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinearRegression.py
250 lines (186 loc) · 7.55 KB
/
LinearRegression.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
"""
Description
----------
Key Parameters
----------
-
Optional Parameters
----------
-
Returns
----------
-
"""
"""
$ Author: Risha $
$ https://github.com/Risha37 $
$ Revision: 1.0 $
TODO
- Add Regularizations Terms 'regularization_term'
- Add different Gradient Descent Algorithms (Stochastic, Mini-Batch)
"""
import numpy as np
class LinearRegression():
def __init__(self):
"""
Description
----------
The following are a set of methods intended for regression in which
the target value is expected to be a linear combination of the features.
"""
pass
def fit(self,
X,
y
):
"""
Description
----------
Fits a linear model using the Normal Equation.
Parameters
----------
- X : array-like of shape (n_samples, n_features)
Training data
- y : array-like of shape (n_samples, 1) or (n_samples, n_targets)
Target data
Returns
----------
- Learned weights : array-like of shape (n_features+1, 1)
Coefficients, Intercept
"""
#Add ones column vector to represent x_0
X = np.append(X, np.ones((len(X), 1)), axis=1)
return (np.linalg.pinv(X.T @ X) @ (X.T @ y))
def predict(self,
X,
optimal_weights
):
"""
Description
----------
Predicts a target given the features value and the learned weights.
Parameters
----------
- X : array-like of shape (n_samples, n_features)
Testing data
- optimal_weight : array-like of shape (n_features+1, 1)
Learned coefficients & intercept after training
Returns
----------
- y predict (y hat) : array-like of shape (n_samples, 1) or (n_samples, n_targets)
Targets predicted using the 'optimal' weights
"""
return ((X @ optimal_weights[:-1]) + optimal_weights[-1])
def GradientDescent(self,
X,
y,
learning_rate: float=0.1,
epochs: int=20,
random_state: int=42,
initial_weight=None,
regularization_term=None,
return_loss: bool=False,
print_results_epoch=[False, None]
):
"""
Description
----------
Fits a linear model using the Batch Gradient Descent Algorithm.
Key Parameters
----------
- X : array-like of shape (n_samples, n_features)
Training data
- y : array-like of shape (n_samples, 1) or (n_samples, n_targets)
Target data
Optional Parameters
----------
- learning_rate : float, default=0.1
Controls how quickly the model is adapted to the problem (alpha)
- epochs : int, default=20
The maximum number of passes over the training data
- random_state : int, default=42
Set random seed to keep consistent results
- initial_weight : array-like of shape (n_features+1, 1), default=None
[Coefficients, Intercept] Custom initialized weights instead of random initialization.
- (WIP)regularization_term : {"Lasso", "Ridge", "ElasticNet", None}, default=None
Imposing a penalty on the size of the coefficients to prevent overfitting
- return_loss : bool, default=False
- print_results_epoch: list [bool, int], default=None
Print results every n epochs
Returns
----------
- W : array-like of shape (n_features+1, 1)
Learned Coefficients & Intercept
- loss : array-like of shape (1, 1), optional
The final loss after n epochs of training
"""
np.random.seed(random_state)
#Initialise random weights
W = np.random.randn(X.shape[1]+1, 1) if initial_weight is None else initial_weight
#Add ones column vector to represent x_0
X = np.append(X, np.ones((len(X), 1)), axis=1)
m = len(X)
for epoch in range(epochs):
if return_loss:
#Vectorized form MSE Cost Function
J = (1/m) * ((X @ W) - y).T @ ((X @ W) - y)
#Vectorized form gradient of the cost function
dJ = (1/m) * (X.T @ (X @ W - y))
#Gradient Descent step
W = W - learning_rate * dJ
if print_results_epoch[0]:
if epoch % print_results_epoch[1] == 0 and return_loss:
print(f"Epoch: {epoch} | Loss: {J} |\nWeights: \n{W}")
elif epoch % print_results_epoch[1] == 0 and not return_loss:
print(f"Epoch: {epoch} |\nWeights: \n{W}")
return (W) if not return_loss else (W, J)
def score(self,
y_true,
y_predict,
criteria: str="R2"
):
"""
Description
----------
(Evaluation metrics) Calculates the {R squared} score.
Key Parameters
----------
- y_true: array-like of shape (n_samples,)
The ground truth target data.
- y_predict: array-like of shape (n_samples,)
The predicted target data.
Optional Parameters
----------
- criteria: {"MSE", "RMSE", "MAE", "R2"}, default='R2'
The criteria used to evaluate the model.
Returns
----------
- score: float
The R2 score between y_true and y_predict.
"""
if criteria == 'R2':
ss_res = np.sum((y_true - y_predict)**2)
ss_tot = np.sum((y_true - np.mean(y_true))**2)
score = 1 - (ss_res / ss_tot)
elif criteria == 'MSE':
score = np.mean((y_true - y_predict)**2)
elif criteria == 'RMSE':
score = np.sqrt(np.mean((y_true - y_predict)**2))
elif criteria == 'MAE':
score = np.mean(np.abs(y_true - y_predict))
return score
from DataProcessing import DataProcessing
lin_reg = LinearRegression()
data_proc = DataProcessing()
np.random.seed(42)
X = np.random.rand(100, 2)
y = (7 * X[:, 0] + 3 * X[:, 1] + 9).reshape(-1, 1) + np.random.randn(100, 1)
X_train, X_test, y_train, y_test = data_proc.train_test_split(X, y)
opt_w0 = lin_reg.fit(X_train, y_train)
opt_w1 = lin_reg.GradientDescent(X_train, y_train, epochs=200)
print(f"Optimal weights (sould be close or equal to W=[7, 3] b=[9])\nNormal Equation = {opt_w0}\nBatch Gradient Descent = {opt_w1}")
y_pred0 = lin_reg.predict(X_test, opt_w0)
y_pred1 = lin_reg.predict(X_test, opt_w1)
score0 = lin_reg.score(y_test, y_pred0)
score1 = lin_reg.score(y_test, y_pred1)
print(f"The Score for the Normal Equation Model = {score0} || Gradient Descent Model = {score1}")