Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

part 4

  • Loading branch information...
commit 5e91186e08b475c7ce773cba3678412b317dd456 1 parent 20e0f57
@Swizec authored
Showing with 22 additions and 20 deletions.
  1. +17 −17 mlclass-ex5/helper.m
  2. +5 −3 mlclass-ex5/polyFeatures.m
View
34 mlclass-ex5/helper.m
@@ -1,23 +1,23 @@
load ('ex5data1.mat');
-% m = Number of examples
-m = size(X, 1);
+p = 8;
-lambda = 0;
-[error_train, error_val] = ...
- learningCurve([ones(m, 1) X], y, ...
- [ones(size(Xval, 1), 1) Xval], yval, ...
- lambda);
+% Map X onto Polynomial Features and Normalize
+X_poly = polyFeatures(X, p);
+[X_poly, mu, sigma] = featureNormalize(X_poly); % Normalize
+X_poly = [ones(m, 1), X_poly]; % Add Ones
-plot(1:m, error_train, 1:m, error_val);
-title('Learning curve for linear regression')
-legend('Train', 'Cross Validation')
-xlabel('Number of training examples')
-ylabel('Error')
-axis([0 13 0 150])
+% Map X_poly_test and normalize (using mu and sigma)
+X_poly_test = polyFeatures(Xtest, p);
+X_poly_test = bsxfun(@minus, X_poly_test, mu);
+X_poly_test = bsxfun(@rdivide, X_poly_test, sigma);
+X_poly_test = [ones(size(X_poly_test, 1), 1), X_poly_test]; % Add Ones
-fprintf('# Training Examples\tTrain Error\tCross Validation Error\n');
-for i = 1:m
- fprintf(' \t%d\t\t%f\t%f\n', i, error_train(i), error_val(i));
-end
+% Map X_poly_val and normalize (using mu and sigma)
+X_poly_val = polyFeatures(Xval, p);
+X_poly_val = bsxfun(@minus, X_poly_val, mu);
+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, :));
View
8 mlclass-ex5/polyFeatures.m
@@ -10,13 +10,15 @@
X_poly = zeros(numel(X), p);
% ====================== 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.
%
-%
-
+%
+for i=1:p
+ X_poly(:,i) = X.^i;
+end
Please sign in to comment.
Something went wrong with that request. Please try again.