-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathlab11.py
More file actions
59 lines (48 loc) · 1.36 KB
/
lab11.py
File metadata and controls
59 lines (48 loc) · 1.36 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import numpy as np
import matplotlib.pyplot as plt
def readData(fin,delimiter):
datas = []
for str in fin:
tmp=str.split(delimiter)
for i in range(0,len(tmp)):
tmp[i] = float(tmp[i])
datas.append(tmp)
return datas
def plotDatas(X,y,marker):
plt.plot(X,y,marker)
plt.xlabel('Population of Cities in 10,000s')
plt.ylabel('Profit in $10,000s')
def MSE(X,y,theta):
m = len(y)
return np.sum((theta.dot(X.T)-y)**2)/(2*m)
def gradient_descent(X, y, theta, alpha, num_iters):
m = len(y)
J_history = np.zeros(num_iters)
for i in range(num_iters):
h = theta.dot(X.T)
errors = h - y
delta = X.T.dot(errors)
theta -= (alpha / m) * delta
J_history[i] = MSE(X, y, theta)
# if i%100==0:
# plotDatas(X[:,1],theta.dot(X.T),'g--')
return (theta, J_history)
fin = open('ex1data1.txt')
ls = readData(fin,',')
data = np.array(ls)
m = len(data)
X = data[:,0]
y = data[:,1]
plotDatas(X,y,'b*')
X = np.stack((np.ones((m,)), X),axis=1)
theta = np.zeros(2)#np.array([-0,1.3])#
# plotDatas(X[:,1],theta.dot(X.T),'r-')
# print(MSE(X,y,theta))
# plt.show()
alpha = 0.01
num_iters = 1500
theta, J_history = gradient_descent(X, y, theta, alpha, num_iters)
# plt.plot(range(0,len(J_history)),np.array(J_history))
# plt.show()
plotDatas(X[:,1],theta.dot(X.T),'r-')
plt.show()