-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinear-regression.py
More file actions
41 lines (28 loc) · 1.04 KB
/
linear-regression.py
File metadata and controls
41 lines (28 loc) · 1.04 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
from statistics import mean
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
style.use('fivethirtyeight')
xs = np.array([1,2,3,4,5,6], dtype=np.float64)
ys = np.array([5,4,6,5,6,7], dtype=np.float64)
def best_fit_slope_and_intercept(xs,ys):
m = ((mean(xs) * mean(ys)) - mean(xs*ys)) / ((mean(xs)**2) - (mean(xs**2)))
b = (mean(ys) - (m*mean(xs)))
return m, b
def squared_error(ys_origin, ys_line):
return sum((ys_line-ys_origin)**2)
def coefficient_of_determination(ys_origin, ys_line):
y_mean_line = [mean(ys_origin) for y in ys_origin]
squared_error_regr = squared_error(ys_origin, ys_line)
squared_error_y_mean = squared_error(ys_origin, y_mean_line)
return 1 - (squared_error_regr / squared_error_y_mean)
m, b = best_fit_slope_and_intercept(xs,ys)
regression_line = [(m*x + b) for x in xs]
predict_x = 7
predict_y = m*predict_x + b
r_squared = coefficient_of_determination(ys, regression_line)
print(r_squared)
plt.scatter(xs,ys)
plt.scatter(predict_x, predict_y)
plt.plot(xs, regression_line)
plt.show()