-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearRegression.py
More file actions
45 lines (39 loc) · 1.22 KB
/
LinearRegression.py
File metadata and controls
45 lines (39 loc) · 1.22 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
#y = 2x + 3
train_data = [(0, 3), (1, 5), (2, 7), (3, 9), (4, 11), (5, 13), (6, 15), (7, 17), (8, 19), (9, 21), (10, 23)]
test_data = [11, 12, 13, 14, 15]
#Gradient Descent
def grad_des_w(w, cost_der_w):
temp_w = w - 0.057*(cost_der_w)
return temp_w
def grad_des_b(b, cost_der_b):
temp_b = b - 0.057*(cost_der_b)
return temp_b
#Squared Mean Cost Function
def cost_func(test_dat, w, b):
total_sq = 0
for data in test_dat:
total_sq += ((w*data[0] + b) - data[1])**2
print(f'Current value: w = {w}, b = {b}')
print(f'Cost Function: {total_sq/(2*len(test_dat))}')
return total_sq/(2*len(test_dat))
#Partial Derivatives
def cost_func_w(test_dat, w, b):
total = 0
for data in test_dat:
total += ((w*data[0] + b) - data[1])*data[0]
return total/len(train_data)
def cost_func_b(test_dat, w, b):
total = 0
for data in test_dat:
total += ((w*data[0] + b) - data[1])
return total/len(train_data)
#Training
w = 0
b = 0
for _ in range(1000):
w = grad_des_w(w, cost_func_w(train_data, w, b))
b = grad_des_b(b, cost_func_b(train_data, w, b))
cost_func(train_data, w, b)
#Testing
for _ in range(5):
print(f'Value for {test_data[_]}: {w*test_data[_] + b}')