-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathlinreg.gradient.py
More file actions
41 lines (31 loc) · 950 Bytes
/
linreg.gradient.py
File metadata and controls
41 lines (31 loc) · 950 Bytes
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
#!/usr/bin/python
import numpy as np
# Implementation based on Ex. 1 in https://www.coursera.org/course/ml
def gradDescent(x, y, theta, alpha, m, nIter):
loss = 0
for i in range(0, nIter):
h = np.dot(x, theta)
loss = (h-y)
gradient = np.dot(x.transpose(), loss) / m
theta = theta - alpha * gradient
cost = np.sum(loss ** 2) / (2 * m)
print("After %d iterations, cost is %f" % (nIter, cost))
return theta
y = np.loadtxt('../data/ex2y.dat')
x = np.loadtxt('../data/ex2x.dat')
on = np.ones(np.shape(x))
# append ones for offset
x = np.column_stack((on,x))
m,n = np.shape(x)
numIter = 10000
# starting values
theta = np.array([5,5])
# learning rate
alpha = 0.05
# gradient descent
theta = gradDescent(x, y, theta, alpha, m, numIter)
print "theta=",theta
# analytical solution
t1 = np.linalg.inv(np.dot(x.transpose(),x))
theta2 = np.dot(np.dot(t1,x.transpose()),y)
print "theta2=",theta2