|
|
@ -42,18 +42,34 @@ error_val = zeros(m, 1);
|
|
|
|
%
|
|
|
|
%
|
|
|
|
% Hint: You can loop over the examples with the following:
|
|
|
|
% Hint: You can loop over the examples with the following:
|
|
|
|
%
|
|
|
|
%
|
|
|
|
% for i = 1:m
|
|
|
|
for i = 1:m
|
|
|
|
% % Compute train/cross validation errors using training examples
|
|
|
|
% Compute train/cross validation errors using training examples
|
|
|
|
% % X(1:i, :) and y(1:i), storing the result in
|
|
|
|
% X(1:i, :) and y(1:i), storing the result in
|
|
|
|
% % error_train(i) and error_val(i)
|
|
|
|
% error_train(i) and error_val(i)
|
|
|
|
% ....
|
|
|
|
|
|
|
|
%
|
|
|
|
X_ = X(1:i,:);
|
|
|
|
% end
|
|
|
|
y_ = y(1:i);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
% Train with regularization
|
|
|
|
|
|
|
|
lambda = 1;
|
|
|
|
|
|
|
|
theta = trainLinearReg(X_, y_, lambda);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
% Compute the error with lambda = 0
|
|
|
|
|
|
|
|
lambda = 0;
|
|
|
|
|
|
|
|
error_train(i) = linearRegCostFunction(X_, y_, theta, lambda);
|
|
|
|
|
|
|
|
error_val(i) = linearRegCostFunction(Xval, yval, theta, lambda);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
%
|
|
|
|
%
|
|
|
|
|
|
|
|
|
|
|
|
% ---------------------- Sample Solution ----------------------
|
|
|
|
% ---------------------- Sample Solution ----------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
% for i = 1:m
|
|
|
|
|
|
|
|
% % Compute train/cross validation errors using training examples
|
|
|
|
|
|
|
|
% % X(1:i, :) and y(1:i), storing the result in
|
|
|
|
|
|
|
|
% % error_train(i) and error_val(i)
|
|
|
|
|
|
|
|
% ....
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|