-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathsimple_linear_regression.py
More file actions
48 lines (40 loc) · 1.53 KB
/
simple_linear_regression.py
File metadata and controls
48 lines (40 loc) · 1.53 KB
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
from __future__ import annotations
import numpy as np
class SimpleLinearRegression:
"""Simple Linear Regression
Parameters:
-----------
learning_rate: float
The step length used when following the negative gradient during training.
"""
def __init__(self, learning_rate: float) -> None:
self.m = 0
self.b = 0
self.learning_rate = learning_rate
def cost_function(self, x: np.ndarray, y: np.ndarray) -> float:
total_error = 0
for i in range(0, len(x)):
total_error += (y[i]-(self.m*x[i]+self.b))**2
return total_error/float(len(x))
def fit(self, x: np.ndarray, y: np.ndarray, num_iterations: int) -> SimpleLinearRegression:
N = float(len(x))
for j in range(num_iterations):
b_gradient = 0
m_gradient = 0
for i in range(0, len(x)):
b_gradient += -(2/N) * (y[i] - ((self.m * x[i]) + self.b))
m_gradient += -(2/N) * x[i] * \
(y[i] - ((self.m * x[i]) + self.b))
self.b -= (self.learning_rate * b_gradient)
self.m -= (self.learning_rate * m_gradient)
return self
def predict(self, xs: np.ndarray) -> list:
return [(self.m * x + self.b) for x in xs]
# Testing functionality
if __name__ == '__main__':
x = np.linspace(0, 100, 50)
delta = np.random.uniform(-10, 10, x.size)
y = 0.5 * x + 3 + delta
model = SimpleLinearRegression(0.0001)
model.fit(x, y, 100)
print('Error:', model.cost_function(x, y))