-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathex5.py
More file actions
289 lines (216 loc) · 7.92 KB
/
ex5.py
File metadata and controls
289 lines (216 loc) · 7.92 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/usr/local/Cellar/python/2.7.6/bin/python
# -*- coding: utf-8 -*-
import sys
import PIL.Image
import scipy.misc, scipy.io, scipy.optimize
from numpy import *
import pylab
from matplotlib import pyplot, cm
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.mlab as mlaba
from util import Util
def featureNormalize( data ):
mu = mean( data, axis=0 )
data_norm = data - mu
sigma = std( data_norm, axis=0, ddof=1 )
data_norm = data_norm / sigma
return data_norm, mu, sigma
def computeCost( theta, X, y, lamda ):
theta = theta.reshape( shape(X)[1], 1 )
m = shape( X )[0]
term1 = X.dot( theta ) - y
left_term = term1.T.dot( term1 ) / (2 * m)
right_term = theta[1:].T.dot( theta[1:] ) * (lamda / (2*m))
J = (left_term + right_term).flatten()[0]
return J
def computeGradient( theta, X, y, lamda ):
theta = theta.reshape( shape(X)[1], 1 )
m = shape( X )[0]
grad = X.dot( theta ) - y
grad = X.T.dot( grad) / m
grad[1:] = grad[1:] + theta[1:] * lamda / m
return grad.flatten()
def linearReg( theta, X, y, lamda ):
cost = computeCost( theta, X, y, lamda )
grad = computeGradient( theta, X, y, lamda )
return grad
def train(X, y, lamda, use_scipy=False):
theta = zeros( (shape(X)[1], 1) )
if use_scipy is True:
result = scipy.optimize.fmin_cg( computeCost, fprime = computeGradient, x0 = theta,
args = (X, y, lamda), maxiter = 200, disp = True, full_output = True )
else:
result = Util.fmincg( f=computeCost, fprime=computeGradient, x0=theta, args=(X, y, lamda), maxiter=200 )
return result[1], result[0]
def plot( X, y, theta ):
m = shape( X )[0]
pyplot.plot( X, y, 'ro' )
pyplot.plot( X, c_[ ones((m, 1)), X ].dot( theta ) )
pyplot.show( block = True )
def learningCurve( X, y, X_val, y_val, lamda ):
m = shape( X )[0]
X = c_[ones((m, 1)), X]
error_train = []
error_val = []
m_val = shape( X_val )[0]
X_val = c_[ones((m_val, 1)), X_val]
for i in range( 0, m ):
cost, theta = train( X[0:i+1,:], y[0:i+1,:], lamda )
error_train.append( computeCost( theta, X[0:i+1,:], y[0:i+1,:], lamda ) )
error_val.append( computeCost( theta, X_val, y_val, lamda ) )
error_train = array(error_train)
error_val = array(error_val)
# number of training examples
temp = array([x for x in range(1, m+1)])
pyplot.ylabel('Error')
pyplot.xlabel('Number of training examples')
pyplot.ylim([-2, 100])
pyplot.xlim([0, 13])
pyplot.plot( temp, error_train, color='b', linewidth=2, label='Train' )
pyplot.plot( temp, error_val, color='g', linewidth=2, label='Cross Validation' )
pyplot.legend()
pyplot.show( block=True )
return error_train, error_val
def polyFeatures( X, p ):
out = copy(X)
for i in range(1, p):
out = c_[out, X**(i+1)]
return out
def validationCurve( X, y, X_val, y_val ):
lamda_vec = array([0, 0.001, 0.003, 0.01, 0.03, 0.1, 0.3, 1.0, 3.0, 10.0]).T
error_train = []
error_val = []
for lamda in lamda_vec:
cost, theta = train( X, y, lamda )
error_train.append( computeCost( theta, X, y, lamda ) )
error_val.append( computeCost( theta, X_val, y_val, lamda ) )
error_train = array( error_train )
error_val = array( error_val )
pyplot.ylabel('Error')
pyplot.xlabel('Lambda')
pyplot.plot( lamda_vec, error_train, 'b', label='Train' )
pyplot.plot( lamda_vec, error_val, 'g', label='Cross Validation' )
pyplot.legend()
pyplot.show( block=True )
return error_train, error_val
def plotFit(min_x, max_x, mu, sigma, theta, p):
x = arange( min_x - 15, max_x + 25, 0.05 )
X_poly = polyFeatures( x, p )
X_poly = (X_poly - mu) / sigma
X_poly = c_[ ones((shape(x)[0], 1)), X_poly ]
pyplot.plot( x, X_poly.dot(theta), linestyle='--', linewidth=3 )
def part1_1():
mat = scipy.io.loadmat( "/Users/saburookita/Downloads/mlclass-ex5-004/mlclass-ex5/ex5data1.mat" )
X, y = mat['X'], mat['y']
pyplot.scatter( X, y, marker='x', c='r', s=30, linewidth=2 )
pyplot.xlim([-55, 45])
pyplot.ylim([-0.5, 45])
pyplot.xlabel('Change in water level(x)')
pyplot.ylabel('Water flowing out of the dam(y)')
pyplot.show()
def part1_2():
mat = scipy.io.loadmat( "/Users/saburookita/Downloads/mlclass-ex5-004/mlclass-ex5/ex5data1.mat" )
X, y = mat['X'], mat['y']
X_val, y_val = mat['Xval'], mat['yval']
X_test, y_test = mat['Xtest'], mat['ytest']
theta = array([[1, 1]]).T
print computeCost( theta, c_[ones((shape(X)[0], 1)), X ] , y, 1.0 )
def part1_3():
mat = scipy.io.loadmat( "/Users/saburookita/Downloads/mlclass-ex5-004/mlclass-ex5/ex5data1.mat" )
X, y = mat['X'], mat['y']
X_val, y_val = mat['Xval'], mat['yval']
X_test, y_test = mat['Xtest'], mat['ytest']
theta = array([[1, 1]]).T
print computeGradient( theta, c_[ones((shape(X)[0], 1)), X ] , y, 1.0 )
def part1_4():
mat = scipy.io.loadmat( "/Users/saburookita/Downloads/mlclass-ex5-004/mlclass-ex5/ex5data1.mat" )
X, y = mat['X'], mat['y']
pyplot.scatter( X, y, marker='x', c='r', s=30, linewidth=2 )
pyplot.xlim([-55, 45])
pyplot.ylim([-5, 45])
pyplot.xlabel('Change in water level(x)')
pyplot.ylabel('Water flowing out of the dam(y)')
lamda = 0.0
X_bias = c_[ones(shape(X)), X]
cost, theta = train( X_bias, y, lamda )
pyplot.plot( X, X_bias.dot( theta ), linewidth=2 )
pyplot.show()
def part2_1():
mat = scipy.io.loadmat( "/Users/saburookita/Downloads/mlclass-ex5-004/mlclass-ex5/ex5data1.mat" )
X, y = mat['X'], mat['y']
X_val, y_val = mat['Xval'], mat['yval']
X_test, y_test = mat['Xtest'], mat['ytest']
lamda = 0.0
learningCurve( X, y, X_val, y_val, lamda )
def part3_1():
mat = scipy.io.loadmat( "/Users/saburookita/Downloads/mlclass-ex5-004/mlclass-ex5/ex5data1.mat" )
X, y = mat['X'], mat['y']
X_val, y_val = mat['Xval'], mat['yval']
X_test, y_test = mat['Xtest'], mat['ytest']
p = 8
m, n = shape( X )
X_poly = polyFeatures( X, p )
X_poly, mu, sigma = featureNormalize( X_poly )
X_poly = c_[ones((m, 1)), X_poly]
X_poly_test = polyFeatures( X_test, p )
X_poly_test = X_poly_test - mu
X_poly_test = X_poly_test / sigma
X_poly_test = c_[ones(( shape(X_poly_test)[0], 1)), X_poly_test]
X_poly_val = polyFeatures( X_val, p )
X_poly_val = X_poly_val - mu
X_poly_val = X_poly_val / sigma
X_poly_val = c_[ones(( shape(X_poly_val)[0], 1)), X_poly_val]
print X_poly[0, :]
lamda = 0.0
cost, theta = train( X_poly, y, lamda )
pyplot.scatter( X, y, marker='x', c='r', s=30, linewidth=2 )
pyplot.xlim([-80, 80])
pyplot.ylim([-60, 40])
pyplot.xlabel('Change in water level(x)')
pyplot.ylabel('Water flowing out of the dam(y)')
pyplot.text( -15, 45, 'Lambda = %.1f' %lamda )
plotFit( min(X), max(X), mu, sigma, theta, p )
pyplot.show()
learningCurve( X_poly, y, X_poly_val, y_val, lamda )
def part3_2():
mat = scipy.io.loadmat( "/Users/saburookita/Downloads/mlclass-ex5-004/mlclass-ex5/ex5data1.mat" )
X, y = mat['X'], mat['y']
X_val, y_val = mat['Xval'], mat['yval']
X_test, y_test = mat['Xtest'], mat['ytest']
p = 8
m, n = shape( X )
X_poly = polyFeatures( X, p )
X_poly, mu, sigma = featureNormalize( X_poly )
X_poly = c_[ones((m, 1)), X_poly]
X_poly_test = polyFeatures( X_test, p )
X_poly_test = X_poly_test - mu
X_poly_test = X_poly_test / sigma
X_poly_test = c_[ones(( shape(X_poly_test)[0], 1)), X_poly_test]
X_poly_val = polyFeatures( X_val, p )
X_poly_val = X_poly_val - mu
X_poly_val = X_poly_val / sigma
X_poly_val = c_[ones(( shape(X_poly_val)[0], 1)), X_poly_val]
print X_poly[0, :]
lamda = 1.0
cost, theta = train( X_poly, y, lamda )
pyplot.scatter( X, y, marker='x', c='r', s=30, linewidth=2 )
pyplot.xlim([-80, 80])
pyplot.ylim([ 0, 160])
pyplot.xlabel('Change in water level(x)')
pyplot.ylabel('Water flowing out of the dam(y)')
pyplot.text( -15, 165, 'Lambda = %.1f' %lamda )
plotFit( min(X), max(X), mu, sigma, theta, p )
pyplot.show()
learningCurve( X_poly, y, X_poly_val, y_val, lamda )
validationCurve( X_poly, y, X_poly_val, y_val )
def main():
set_printoptions(precision=6, linewidth=200)
part1_1()
part1_2()
part1_3()
part1_4()
part2_1()
part3_1()
part3_2()
if __name__ == '__main__':
main()