-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLineerRegression.py
More file actions
65 lines (54 loc) · 1.3 KB
/
Copy pathLineerRegression.py
File metadata and controls
65 lines (54 loc) · 1.3 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
Liimport dataset
import matplotlib.pyplot as plt
import numpy as np
def derivQ0(Q,x,y):
V=0.0
M= len(x)
for i in range(M):
V = x[i]*((h(x[i],Q) - y[i])) + V
F = (1/M)*V
return F
def derivQ1(Q,x,y):
V = 0.0
M= len(x)
for i in range(M):
V = ((h(x[i], Q) - y[i])) + V
F = (1 / M) * V
return F
def h(x,Q):
F = Q[0]*x + Q[1]
return F
def J(Q,x,y):
V = 0.0
M= len(x)
for i in range(M):
V = (h(x[i],Q) - y[i])**2 + V
F = (1/(2*M))*V
return F
def plotting(Q,x,y):
plt.subplot(321)
plt.scatter(x, y, s=2, marker=">")
axes = plt.gca()
x_vals = np.array(axes.get_xlim())
y_vals = Q[1] + Q[0] * x_vals
plt.plot(x_vals, y_vals, '-', color='blue')
def plotting2(Q,x,y):
plt.subplot(321)
plt.scatter(x, y, s=2, marker=">")
axes = plt.gca()
x_vals = np.array(axes.get_xlim())
y_vals = Q[1] + Q[0] * x_vals
plt.plot(x_vals, y_vals, '-', color='red')
plt.show()
def linear_reg(X,Y,alfa=0.001, train_rate=10000):
Q = [1.0,1.0]
for i in range(train_rate):
Q[0] = Q[0] - alfa * derivQ0(Q,X,Y)
Q[1] = Q[1] - alfa * derivQ1(Q,X,Y)
Q = [Q[0],Q[1]]
if i%1000 == 1:
plotting(Q, X, Y)
plotting2(Q,X,Y)
X, Y= dataset.dataset()
print(X)
linear_reg(X,Y)