-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinear_regression_1d.py
73 lines (49 loc) · 1.27 KB
/
linear_regression_1d.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
#Verifying linear regression formulas through code
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
#Load data/data_1d.csv
D = pd.read_csv("./data/data_1d.csv", header=None)
A = np.array(D)
X = A[:, 0]
Y = A[:, 1]
#Plotting the data
#Plot is in a line shape (Yi = mXi + b)
#Goal is to derive m and b using linear regression formulas
#Square of X
Xsq = (X**2)
#Mean of square of X
meanXsq = Xsq.mean()
#Mean of X
meanX = X.mean()
#Square of Mean of X
SqMeanX = meanX ** 2
#Denominator formula for calc a and b
denominator = meanXsq - SqMeanX
#Mean of Y
meanY = Y.mean()
#Calc...mean XY
XY = X * Y
meanXY = XY.mean()
#Slope m or a
m = (meanXY - (meanX * meanY))/denominator
#Calculating b the simple way
b = meanY - m*meanX
#Calculating b the another way
b2 = (meanY*meanXsq - meanX*meanXY)/ denominator
#b1 and b2 should be about equal
print(b2)
print(b)
#Plotting the calculated line of best fit
bestFit = m*X + b2
#Calculating Rsquared
ERRi = Y - bestFit
SSTOTi = Y - Y.mean()
SSresidual = ERRi.dot(ERRi)
SStotal = SSTOTi.dot(SSTOTi)
Rsquare = 1 - SSresidual/SStotal
print(Rsquare)
#Plotting our predict model along with the
plt.scatter(X, Y)
plt.plot(X, bestFit)
plt.show()