-
Notifications
You must be signed in to change notification settings - Fork 0
/
linear_regression.py
54 lines (41 loc) · 1.38 KB
/
linear_regression.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
import matplotlib.pyplot as plt
import random
class LinearRegression:
def __init__(self, data):
self.x = data[0]
self.y = data[1]
self._a = self.a
self._b = self.b
self._line = self.line
@property
def a(self):
self._a = sum(self.y) * sum(x**2 for x in self.x) - sum(self.x) * sum(x*y for x, y in zip(self.x, self.y))
self._a /= len(self.x) * sum(x**2 for x in self.x) - sum(self.x)**2
return self._a
@property
def b(self):
self._b = len(self.x) * sum(x*y for x, y in zip(self.x, self.y)) - sum(self.x) * sum(self.y)
self._b /= len(self.x) * sum(x**2 for x in self.x) - sum(self.x)**2
return self._b
@property
def line(self):
self._line = [self.a + self.b * x for x in self.x]
return self._line
def plot_line(self):
plt.style.use('fivethirtyeight')
plt.scatter(self.x, self.y, c='red')
plt.plot(self.x, self.line, c='green')
plt.show()
def create_dataset(lenght, variance, step=2):
x, y = [x for x in range(lenght)], []
value = 1
for i in range(lenght):
y.append(value + random.randrange(-variance, variance))
value += step
return x, y
def main():
data = create_dataset(20, 2)
linear_regression = LinearRegression(data)
linear_regression.plot_line()
if __name__ == '__main__':
main()