-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv_regression.py
126 lines (90 loc) · 2.62 KB
/
csv_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
import matplotlib.pyplot as plot
import csv
csv_file = open('dose_figures.csv')
csv_reader = csv.reader(csv_file)
x_train = [0.0]*20
y_train = [0.0]*20
for i, row in enumerate(csv_reader):
x_train[i] = float(row[0])
y_train[i] = float(row[1])
print(x_train)
print(y_train)
f01 = plot.figure(1)
plot.scatter(x_train, y_train, c = 'b')
plot.xlabel('mass of drugs injected i.v. (mg)')
plot.ylabel('mass of drugs in brain (\u03BCg)')
ax = plot.gca()
ax.set_xlim(0, 10)
ax.set_ylim(0, 2500)
# plot.show()
def linear_equation(x, w, b):
y = w * x + b
return y
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
def 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
def gradient_descent(x_train, y_train, w_init, b_init, alpha, N_iterations, model, cost_function, gradient_function):
J_log = []
p_log = []
w = w_init
b = b_init
for i in range(N_iterations):
dJ_dw, dJ_db = gradient_function(x_train, y_train, model, w, b)
w = w - alpha * dJ_dw
b = b - alpha * dJ_db
J_log.append(cost_function(x_train, y_train, model, w, b))
p_log.append([w,b])
return w, b, J_log, p_log
w_init = 0
b_init = 0
N_iterations = 10
alpha = 0.01
w_final, b_final, J_log, p_log = gradient_descent(x_train, y_train, w_init, b_init, alpha, N_iterations, linear_equation, cost_function, gradient)
N_train = len(x_train)
y_pred = N_train*[0.0]
for i in range(N_train):
y_pred[i] = linear_equation(x_train[i], w_final, b_final)
print(f'w_final and b_final: {w_final}, {b_final}')
w_log, b_log = list(zip(*p_log))
print(len(J_log))
print(J_log)
f02 = plot.figure(2)
plot.plot(x_train, y_pred, c = 'r', label='model prediction')
plot.scatter(x_train, y_train, c = 'b', label='training data')
plot.xlabel('mass of drugs injected i.v. (mg)')
plot.ylabel('mass of drugs in brain (\u03BCg)')
ax = plot.gca()
# ax.set_xlim(0, 10)
# ax.set_ylim(0, 2500)
f03 = plot.figure(3)
plot.plot(w_log)
plot.xlabel('number of iterations')
plot.ylabel('w parameter')
f04 = plot.figure(4)
plot.plot(b_log)
plot.xlabel('number of iterations')
plot.ylabel('b parameter')
f05 = plot.figure(5)
plot.plot(J_log)
plot.xlabel('number of iterations')
plot.ylabel('cost function')
plot.show()