Skip to content

Commit 24add7d

Browse files
solves week 6 ex 5
1 parent e6b52f1 commit 24add7d

29 files changed

+3641
-1
lines changed

README.md

+39-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ __Instructor__: Andrew Ng.
1010
| [Week 3](#week-3) | [[Quizzes]](#quizzes-2) [[Programming Assignment]](#programming-exercises-1) |
1111
| [Week 4](#week-4) | [[Quizzes]](#quizzes-3) [[Programming Assignment]](#programming-exercises-2) |
1212
| [Week 5](#week-5) | [[Quizzes]](#quizzes-4) [[Programming Assignment]](#programming-exercises-3) |
13+
| [Week 6](#week-6) | [[Quizzes]](#quizzes-5) [[Programming Assignment]](#programming-exercises-4) |
14+
| [Week 7](#week-7) | [[Quizzes]](#quizzes-6) [[Programming Assignment]](#programming-exercises-5) |
15+
| [Week 8](#week-8) | [[Quizzes]](#quizzes-7) [[Programming Assignment]](#programming-exercises-6) |
16+
| [Week 9](#week-9) | [[Quizzes]](#quizzes-8) [[Programming Assignment]](#programming-exercises-7) |
17+
| [Week 10](#week-10) | [[Quizzes]](#quizzes-9) [[Programming Assignment]](#programming-exercises-8) |
18+
| [Week 11](#week-11) | [[Quizzes]](#quizzes-10) [[Programming Assignment]](#programming-exercises-9) |
1319

1420

1521
## Week 1
@@ -65,7 +71,7 @@ __Instructor__: Andrew Ng.
6571
- [Neural Networks: Learning](week5/neural-networks-quiz.md)
6672

6773
### Programming Exercises
68-
- [Questions and Expalanations](week5/ex4.pdf)
74+
- [Questions and Explanations](week5/ex4.pdf)
6975
- [Exercise 4](week5/ex4)
7076
- [Feedforward and Cost Function](week5/ex4/nnCostFunction.m)
7177
- [Regularized Cost Function](week5/ex4/nnCostFunction.m)
@@ -77,5 +83,37 @@ __Instructor__: Andrew Ng.
7783

7884
## Week 6
7985
### Quizzes
86+
- [Advice for Applied Machine Learning](week6/advice-for-applying-machine-learning.md)
87+
88+
### Programming Exercises
89+
- [Questions and Explanations](week6/ex5.pdf)
90+
- [Exercise 5](week6/ex5)
91+
- [Regularized Linear Regression Cost Function](week6/ex5/linearRegCostFunction.m)
92+
- [Generate Learning Curve](week6/ex5/learningCurve.m)
93+
- [Maps Data into Polynomial Feature Space](week6/ex5/polyFeatures.m)
94+
- [Generates a Cross Validation Curve](week6/ex5/validationCurve.m)
95+
96+
## Week 7
97+
### Quizzes
98+
99+
### Programming Exercises
100+
101+
## Week 8
102+
### Quizzes
103+
104+
### Programming Exercises
105+
106+
## Week 9
107+
### Quizzes
108+
109+
### Programming Exercises
110+
111+
## Week 10
112+
### Quizzes
113+
114+
### Programming Exercises
115+
116+
## Week 11
117+
### Quizzes
80118

81119
### Programming Exercises

test.m

+26
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,26 @@
1111
J = (1 / (2 * trainingSampleSize)) * sum((data * hypothesis - result) .^ 2);
1212
endfunction
1313

14+
function J = regularizedRegressionCost(theta, X, y, regularizationConstant)
15+
J = regressionCost(theta, X, y);
16+
trainingSampleSize = length(y);
17+
J += (regularizationConstant / (2 * trainingSampleSize)) * sum(theta .^ 2);
18+
endfunction
19+
1420
function grad = regressionGradient(hypothesis, X, y)
1521
trainingSampleSize = size(X, 1);
1622
grad = (1 / trainingSampleSize) * (((X * hypothesis) - y)' * X)';
1723
endfunction
1824

25+
function grad = regularizedRegressionGradient(theta, X, y, lambda)
26+
grad = regressionGradient(theta, X, y);
27+
trainingSampleSize = size(X, 1);
28+
features = size(X, 2) - 1;
29+
regularizationMask = (lambda / trainingSampleSize) * ones(features + 1, 1);
30+
regularizationMask(1) = 0;
31+
grad += regularizationMask .* theta;
32+
endfunction
33+
1934
function [minCost, hypothesis, costMemory] = gradientDescent(hypothesis, X, y, iterations, learningRate)
2035
costMemory = [];
2136
for i = 1:iterations
@@ -44,3 +59,14 @@
4459
disp(hypothesis);
4560

4661
plot(costMemory);
62+
63+
disp('gradient');
64+
disp(regularizedRegressionGradient(hypothesis, data, y, 10));
65+
66+
clc;
67+
68+
X = [1 1 ; 1 2 ; 1 3];
69+
y = [1 ; 2 ; 3];
70+
lambda = 0.05;
71+
theta = trainLineaReg(X, y, lambda);
72+
disp(theta);

week6/ex5.pdf

183 KB
Binary file not shown.

week6/ex5/ex5.m

+220
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
%% Machine Learning Online Class
2+
% Exercise 5 | Regularized Linear Regression and Bias-Variance
3+
%
4+
% Instructions
5+
% ------------
6+
%
7+
% This file contains code that helps you get started on the
8+
% exercise. You will need to complete the following functions:
9+
%
10+
% linearRegCostFunction.m
11+
% learningCurve.m
12+
% validationCurve.m
13+
%
14+
% For this exercise, you will not need to change any code in this file,
15+
% or any other files other than those mentioned above.
16+
%
17+
18+
%% Initialization
19+
clear ; close all; clc
20+
21+
%% =========== Part 1: Loading and Visualizing Data =============
22+
% We start the exercise by first loading and visualizing the dataset.
23+
% The following code will load the dataset into your environment and plot
24+
% the data.
25+
%
26+
27+
% Load Training Data
28+
fprintf('Loading and Visualizing Data ...\n')
29+
30+
% Load from ex5data1:
31+
% You will have X, y, Xval, yval, Xtest, ytest in your environment
32+
load ('ex5data1.mat');
33+
34+
% m = Number of examples
35+
m = size(X, 1);
36+
37+
% Plot training data
38+
plot(X, y, 'rx', 'MarkerSize', 10, 'LineWidth', 1.5);
39+
xlabel('Change in water level (x)');
40+
ylabel('Water flowing out of the dam (y)');
41+
42+
fprintf('Program paused. Press enter to continue.\n');
43+
pause;
44+
45+
%% =========== Part 2: Regularized Linear Regression Cost =============
46+
% You should now implement the cost function for regularized linear
47+
% regression.
48+
%
49+
50+
theta = [1 ; 1];
51+
J = linearRegCostFunction([ones(m, 1) X], y, theta, 1);
52+
53+
fprintf(['Cost at theta = [1 ; 1]: %f '...
54+
'\n(this value should be about 303.993192)\n'], J);
55+
56+
fprintf('Program paused. Press enter to continue.\n');
57+
pause;
58+
59+
%% =========== Part 3: Regularized Linear Regression Gradient =============
60+
% You should now implement the gradient for regularized linear
61+
% regression.
62+
%
63+
64+
theta = [1 ; 1];
65+
[J, grad] = linearRegCostFunction([ones(m, 1) X], y, theta, 1);
66+
67+
fprintf(['Gradient at theta = [1 ; 1]: [%f; %f] '...
68+
'\n(this value should be about [-15.303016; 598.250744])\n'], ...
69+
grad(1), grad(2));
70+
71+
fprintf('Program paused. Press enter to continue.\n');
72+
pause;
73+
74+
75+
%% =========== Part 4: Train Linear Regression =============
76+
% Once you have implemented the cost and gradient correctly, the
77+
% trainLinearReg function will use your cost function to train
78+
% regularized linear regression.
79+
%
80+
% Write Up Note: The data is non-linear, so this will not give a great
81+
% fit.
82+
%
83+
84+
% Train linear regression with lambda = 0
85+
lambda = 0;
86+
[theta] = trainLinearReg([ones(m, 1) X], y, lambda);
87+
88+
% Plot fit over the data
89+
plot(X, y, 'rx', 'MarkerSize', 10, 'LineWidth', 1.5);
90+
xlabel('Change in water level (x)');
91+
ylabel('Water flowing out of the dam (y)');
92+
hold on;
93+
plot(X, [ones(m, 1) X]*theta, '--', 'LineWidth', 2)
94+
hold off;
95+
96+
fprintf('Program paused. Press enter to continue.\n');
97+
pause;
98+
99+
100+
%% =========== Part 5: Learning Curve for Linear Regression =============
101+
% Next, you should implement the learningCurve function.
102+
%
103+
% Write Up Note: Since the model is underfitting the data, we expect to
104+
% see a graph with "high bias" -- Figure 3 in ex5.pdf
105+
%
106+
107+
lambda = 0;
108+
[error_train, error_val] = ...
109+
learningCurve([ones(m, 1) X], y, ...
110+
[ones(size(Xval, 1), 1) Xval], yval, ...
111+
lambda);
112+
113+
plot(1:m, error_train, 1:m, error_val);
114+
title('Learning curve for linear regression')
115+
legend('Train', 'Cross Validation')
116+
xlabel('Number of training examples')
117+
ylabel('Error')
118+
axis([0 13 0 150])
119+
120+
fprintf('# Training Examples\tTrain Error\tCross Validation Error\n');
121+
for i = 1:m
122+
fprintf(' \t%d\t\t%f\t%f\n', i, error_train(i), error_val(i));
123+
end
124+
125+
fprintf('Program paused. Press enter to continue.\n');
126+
pause;
127+
128+
%% =========== Part 6: Feature Mapping for Polynomial Regression =============
129+
% One solution to this is to use polynomial regression. You should now
130+
% complete polyFeatures to map each example into its powers
131+
%
132+
133+
p = 8;
134+
135+
% Map X onto Polynomial Features and Normalize
136+
X_poly = polyFeatures(X, p);
137+
[X_poly, mu, sigma] = featureNormalize(X_poly); % Normalize
138+
X_poly = [ones(m, 1), X_poly]; % Add Ones
139+
140+
% Map X_poly_test and normalize (using mu and sigma)
141+
X_poly_test = polyFeatures(Xtest, p);
142+
X_poly_test = bsxfun(@minus, X_poly_test, mu);
143+
X_poly_test = bsxfun(@rdivide, X_poly_test, sigma);
144+
X_poly_test = [ones(size(X_poly_test, 1), 1), X_poly_test]; % Add Ones
145+
146+
% Map X_poly_val and normalize (using mu and sigma)
147+
X_poly_val = polyFeatures(Xval, p);
148+
X_poly_val = bsxfun(@minus, X_poly_val, mu);
149+
X_poly_val = bsxfun(@rdivide, X_poly_val, sigma);
150+
X_poly_val = [ones(size(X_poly_val, 1), 1), X_poly_val]; % Add Ones
151+
152+
fprintf('Normalized Training Example 1:\n');
153+
fprintf(' %f \n', X_poly(1, :));
154+
155+
fprintf('\nProgram paused. Press enter to continue.\n');
156+
pause;
157+
158+
159+
160+
%% =========== Part 7: Learning Curve for Polynomial Regression =============
161+
% Now, you will get to experiment with polynomial regression with multiple
162+
% values of lambda. The code below runs polynomial regression with
163+
% lambda = 0. You should try running the code with different values of
164+
% lambda to see how the fit and learning curve change.
165+
%
166+
167+
lambda = 0;
168+
[theta] = trainLinearReg(X_poly, y, lambda);
169+
170+
% Plot training data and fit
171+
figure(1);
172+
plot(X, y, 'rx', 'MarkerSize', 10, 'LineWidth', 1.5);
173+
plotFit(min(X), max(X), mu, sigma, theta, p);
174+
xlabel('Change in water level (x)');
175+
ylabel('Water flowing out of the dam (y)');
176+
title (sprintf('Polynomial Regression Fit (lambda = %f)', lambda));
177+
178+
figure(2);
179+
[error_train, error_val] = ...
180+
learningCurve(X_poly, y, X_poly_val, yval, lambda);
181+
plot(1:m, error_train, 1:m, error_val);
182+
183+
title(sprintf('Polynomial Regression Learning Curve (lambda = %f)', lambda));
184+
xlabel('Number of training examples')
185+
ylabel('Error')
186+
axis([0 13 0 100])
187+
legend('Train', 'Cross Validation')
188+
189+
fprintf('Polynomial Regression (lambda = %f)\n\n', lambda);
190+
fprintf('# Training Examples\tTrain Error\tCross Validation Error\n');
191+
for i = 1:m
192+
fprintf(' \t%d\t\t%f\t%f\n', i, error_train(i), error_val(i));
193+
end
194+
195+
fprintf('Program paused. Press enter to continue.\n');
196+
pause;
197+
198+
%% =========== Part 8: Validation for Selecting Lambda =============
199+
% You will now implement validationCurve to test various values of
200+
% lambda on a validation set. You will then use this to select the
201+
% "best" lambda value.
202+
%
203+
204+
[lambda_vec, error_train, error_val] = ...
205+
validationCurve(X_poly, y, X_poly_val, yval);
206+
207+
close all;
208+
plot(lambda_vec, error_train, lambda_vec, error_val);
209+
legend('Train', 'Cross Validation');
210+
xlabel('lambda');
211+
ylabel('Error');
212+
213+
fprintf('lambda\t\tTrain Error\tValidation Error\n');
214+
for i = 1:length(lambda_vec)
215+
fprintf(' %f\t%f\t%f\n', ...
216+
lambda_vec(i), error_train(i), error_val(i));
217+
end
218+
219+
fprintf('Program paused. Press enter to continue.\n');
220+
pause;

week6/ex5/ex5data1.mat

1.29 KB
Binary file not shown.

week6/ex5/featureNormalize.m

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function [X_norm, mu, sigma] = featureNormalize(X)
2+
%FEATURENORMALIZE Normalizes the features in X
3+
% FEATURENORMALIZE(X) returns a normalized version of X where
4+
% the mean value of each feature is 0 and the standard deviation
5+
% is 1. This is often a good preprocessing step to do when
6+
% working with learning algorithms.
7+
8+
mu = mean(X);
9+
X_norm = bsxfun(@minus, X, mu);
10+
11+
sigma = std(X_norm);
12+
X_norm = bsxfun(@rdivide, X_norm, sigma);
13+
end

0 commit comments

Comments
 (0)