-
Notifications
You must be signed in to change notification settings - Fork 0
/
carbon.py
84 lines (67 loc) · 2.24 KB
/
carbon.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
import random
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
def compute_cost_function(m, t0, t1, x, y):
"""
Cost Function J(t0, t1)
"""
return 1/2/m * sum([(t0 + t1* np.asarray([x[i]]) - y[i])**2 for i in range(m)])
def gradient_descent(alpha, x, y, ep=0.0001, max_iter=1500):
converged = False
iter = 0
m = x.shape[0] # number of samples
# Initialize theta at (0, 0)
t0 = 0
t1 = 0
# total error, J(theta)
J = compute_cost_function(m, t0, t1, x, y)
print('J=', J);
# Iterate Loop
num_iter = 0
while not converged:
# for each training sample, compute the gradient (d/d_theta j(theta))
grad0 = 1.0/m * sum([(t0 + t1*np.asarray([x[i]]) - y[i]) for i in range(m)])
grad1 = 1.0/m * sum([(t0 + t1*np.asarray([x[i]]) - y[i])*np.asarray([x[i]]) for i in range(m)])
# update the theta_temp
temp0 = t0 - alpha * grad0
temp1 = t1 - alpha * grad1
# update theta
t0 = temp0
t1 = temp1
# mean squared error
e = compute_cost_function(m, t0, t1, x, y)
print ('J = ', e)
J = e # update error
iter += 1 # update iter
if iter == max_iter:
print ('Max interactions exceeded!')
converged = True
return t0,t1
def plot_cost_function(x, y, m):
t0 = list(range(0, x.shape[0]))
j_values = []
for i in range(len(t0)):
j_values.append(compute_cost_function(m, i, i, x, y)[0])
print ('j_values', len(j_values), len(x), len(y))
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot(x, y, j_values, label='parametric curve')
ax.legend()
plt.show()
if __name__ == '__main__':
df = pd.read_csv('data/ex1data1.txt', names=['x','y'])
x = df['x']
y = df['y']
alpha = 0.01 # learning rate
ep = 0.01 # convergence criteria
# call gredient decent, and get intercept(=theta0) and slope(=theta1)
theta0, theta1 = gradient_descent(alpha, x, y, ep, max_iter=1500)
print ('theta0 = ' + str(theta0)+' theta1 = '+str(theta1))
# plot
for i in range(x.shape[0]):
y_predict = theta0 + theta1*x
plt.plot(x,y,'o')
plt.plot(x,y_predict,'k-')
plt.show()
print ("Done!")