-
Notifications
You must be signed in to change notification settings - Fork 0
/
linear_regression.py
139 lines (107 loc) · 3.44 KB
/
linear_regression.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# regression test
import matplotlib.pyplot as plot
# determine the objective -----
# Setup training data -----
x_train = [1.0, 2.0, 3.0] # mg of drugs injected in blood
y_train = [40.0, 60.0, 80.0] # µg of drugs reaching brain tissue
plot.scatter(x_train, y_train)
plot.xlabel('Mass of drugs injected i.v. (mg)')
plot.ylabel('Mass of drugs in brain (\u03BCg)')
ax = plot.gca()
ax.set_xlim(0,5)
ax.set_ylim(0,100)
# plot.show()
# Design the model -----
# Let's try the model y = w * x + b
# where x is in the input and y is the output
def linear_equation(x, w, b):
y = w * x + b
return y
w = 2
b = 5
print(f'(w, b) = ({w},{b})')
N_train = len(x_train)
y_pred = N_train*[0]
for i in range(N_train):
y_pred[i] = linear_equation(x_train[i], w, b)
# Design the cost function -----
plot.plot(x_train, y_pred, c = 'r', label='model prediction')
plot.scatter(x_train, y_train, c = 'b', label = 'true values')
plot.xlabel('Mass of drugs injected i.v. (mg)')
plot.ylabel('Mass of drugs in brain (\u03BCg)')
ax = plot.gca()
ax.set_xlim(0,5)
ax.set_ylim(0,100)
# plot.show()
def cost_function(x_train, y_train, model, w, b):
N_train = len(x_train)
sum_cost = 0
for i in range(N_train):
y_pred = model(x_train[i], w, b)
cost = (y_pred - y_train[i]) ** 2
sum_cost = sum_cost + cost
sum_cost = (1/(2*N_train)) * sum_cost
return sum_cost
w = 2
b = 5
cost = cost_function(x_train, y_train, linear_equation, w, b)
print(f'cost for (w, b) = ({w},{b}) is {cost}')
b = 10
cost = cost_function(x_train, y_train, linear_equation, w, b)
print(f'cost for (w, b) = ({w},{b}) is {cost}')
# Optimise the model ----
def compute_gradient(x_train, y_train, model, w, b):
N_train = len(x_train)
dJ_dw = 0
dJ_db = 0
for i in range(N_train):
y_pred = model(x_train[i], w, b)
dJ_dw_i = (y_pred - y_train[i]) * x_train[i]
dJ_db_i = (y_pred - y_train[i])
dJ_dw += dJ_dw_i
dJ_db += dJ_db_i
dJ_dw = dJ_dw / N_train
dJ_db = dJ_db / N_train
return dJ_dw, dJ_db
w = 2
b = 5
dJ_dw, dJ_db = compute_gradient(x_train, y_train, linear_equation, w, b)
print(f'gradient at (w, b) = ({w},{b}) is {dJ_dw, dJ_db}')
w = 4
b = 7
dJ_dw, dJ_db = compute_gradient(x_train, y_train, linear_equation, w, b)
print(f'gradient at (w, b) = ({w},{b}) is {dJ_dw, dJ_db}')
w = 8
b = 14
dJ_dw, dJ_db = compute_gradient(x_train, y_train, linear_equation, w, b)
print(f'gradient at (w, b) = ({w},{b}) is {dJ_dw, dJ_db}')
cost = cost_function(x_train, y_train, linear_equation, w, b)
print(f'cost for (w, b) = ({w},{b}) is {cost}')
w = 10
b = 20
dJ_dw, dJ_db = compute_gradient(x_train, y_train, linear_equation, w, b)
print(f'gradient at (w, b) = ({w},{b}) is {dJ_dw, dJ_db}')
cost = cost_function(x_train, y_train, linear_equation, w, b)
print(f'cost for (w, b) = ({w},{b}) is {cost}')
w = 20
b = 20
dJ_dw, dJ_db = compute_gradient(x_train, y_train, linear_equation, w, b)
print(f'gradient at (w, b) = ({w},{b}) is {dJ_dw, dJ_db}')
cost = cost_function(x_train, y_train, linear_equation, w, b)
print(f'cost for (w, b) = ({w},{b}) is {cost}')
# Use the model to make predictions -----
w = 20
b = 20
print(f'(w, b) = ({w},{b})')
N_train = len(x_train)
y_pred = N_train*[0]
for i in range(N_train):
y_pred[i] = linear_equation(x_train[i], w, b)
plot.plot(x_train, y_pred, c = 'r', label='model prediction')
plot.scatter(x_train, y_train, c = 'b', label = 'true values')
plot.xlabel('Mass of drugs injected i.v. (mg)')
plot.ylabel('Mass of drugs in brain (\u03BCg)')
ax = plot.gca()
ax.set_xlim(0,5)
ax.set_ylim(0,100)
plot.show()