Skip to content

Commit

Permalink
part 4
Browse files Browse the repository at this point in the history
  • Loading branch information
Swizec committed Nov 21, 2011
1 parent 20e0f57 commit 5e91186
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
34 changes: 17 additions & 17 deletions mlclass-ex5/helper.m
@@ -1,23 +1,23 @@
load ('ex5data1.mat'); load ('ex5data1.mat');


% m = Number of examples p = 8;
m = size(X, 1);


lambda = 0; % Map X onto Polynomial Features and Normalize
[error_train, error_val] = ... X_poly = polyFeatures(X, p);
learningCurve([ones(m, 1) X], y, ... [X_poly, mu, sigma] = featureNormalize(X_poly); % Normalize
[ones(size(Xval, 1), 1) Xval], yval, ... X_poly = [ones(m, 1), X_poly]; % Add Ones
lambda);


plot(1:m, error_train, 1:m, error_val); % Map X_poly_test and normalize (using mu and sigma)
title('Learning curve for linear regression') X_poly_test = polyFeatures(Xtest, p);
legend('Train', 'Cross Validation') X_poly_test = bsxfun(@minus, X_poly_test, mu);
xlabel('Number of training examples') X_poly_test = bsxfun(@rdivide, X_poly_test, sigma);
ylabel('Error') X_poly_test = [ones(size(X_poly_test, 1), 1), X_poly_test]; % Add Ones
axis([0 13 0 150])


fprintf('# Training Examples\tTrain Error\tCross Validation Error\n'); % Map X_poly_val and normalize (using mu and sigma)
for i = 1:m X_poly_val = polyFeatures(Xval, p);
fprintf(' \t%d\t\t%f\t%f\n', i, error_train(i), error_val(i)); X_poly_val = bsxfun(@minus, X_poly_val, mu);
end X_poly_val = bsxfun(@rdivide, X_poly_val, sigma);
X_poly_val = [ones(size(X_poly_val, 1), 1), X_poly_val]; % Add Ones


fprintf('Normalized Training Example 1:\n');
fprintf(' %f \n', X_poly(1, :));
8 changes: 5 additions & 3 deletions mlclass-ex5/polyFeatures.m
Expand Up @@ -10,13 +10,15 @@
X_poly = zeros(numel(X), p); X_poly = zeros(numel(X), p);


% ====================== YOUR CODE HERE ====================== % ====================== YOUR CODE HERE ======================
% Instructions: Given a vector X, return a matrix X_poly where the p-th % Instructions: Given a vector X, return a matrix X_poly where the p-th
% column of X contains the values of X to the p-th power. % column of X contains the values of X to the p-th power.
% %
% %





for i=1:p
X_poly(:,i) = X.^i;
end






Expand Down

0 comments on commit 5e91186

Please sign in to comment.