forked from sarvasvkulpati/LinearRegression
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearRegression.py
More file actions
75 lines (43 loc) · 1.55 KB
/
Copy pathLinearRegression.py
File metadata and controls
75 lines (43 loc) · 1.55 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
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
from sklearn.datasets import make_regression
import matplotlib.pyplot as plt
import numpy as np
X, y= make_regression(n_samples=100, n_features=1, noise=0.4, bias=50)
def plotLine(theta0, theta1, X, y):
max_x = np.max(X) + 100
min_x = np.min(X) - 100
xplot = np.linspace(min_x, max_x, 1000)
yplot = theta0 + theta1 * xplot
plt.plot(xplot, yplot, color='#58b970', label='Regression Line')
plt.scatter(X,y)
plt.axis([-10, 10, 0, 200])
plt.show()
def hypothesis(theta0, theta1, x):
return theta0 + (theta1*x)
def cost(theta0, theta1, X, y):
costValue = 0
for (xi, yi) in zip(X, y):
costValue += 0.5 * ((hypothesis(theta0, theta1, xi) - yi)**2)
return costValue
def derivatives(theta0, theta1, X, y):
dtheta0 = 0
dtheta1 = 0
for (xi, yi) in zip(X, y):
dtheta0 += hypothesis(theta0, theta1, xi) - yi
dtheta1 += (hypothesis(theta0, theta1, xi) - yi)*xi
dtheta0 /= len(X)
dtheta1 /= len(X)
return dtheta0, dtheta1
def updateParameters(theta0, theta1, X, y, alpha):
dtheta0, dtheta1 = derivatives(theta0, theta1, X, y)
theta0 = theta0 - (alpha * dtheta0)
theta1 = theta1 - (alpha * dtheta1)
return theta0, theta1
def LinearRegression(X, y):
theta0 = np.random.rand()
theta1 = np.random.rand()
for i in range(0, 1000):
if i % 100 == 0:
plotLine(theta0, theta1, X, y)
# print(cost(theta0, theta1, X, y))
theta0, theta1 = updateParameters(theta0, theta1, X, y, 0.005)
LinearRegression(X, y)