Path: blob/master/Week 3/Programming Assignment - 2/machine-learning-ex2/ex2/ex2_reg.m
863 views
%% Machine Learning Online Class - Exercise 2: Logistic Regression1%2% Instructions3% ------------4%5% This file contains code that helps you get started on the second part6% of the exercise which covers regularization with logistic regression.7%8% You will need to complete the following functions in this exericse:9%10% sigmoid.m11% costFunction.m12% predict.m13% costFunctionReg.m14%15% For this exercise, you will not need to change any code in this file,16% or any other files other than those mentioned above.17%1819%% Initialization20clear ; close all; clc2122%% Load Data23% The first two columns contains the X values and the third column24% contains the label (y).2526data = load('ex2data2.txt');27X = data(:, [1, 2]); y = data(:, 3);2829plotData(X, y);3031% Put some labels32hold on;3334% Labels and Legend35xlabel('Microchip Test 1')36ylabel('Microchip Test 2')3738% Specified in plot order39legend('y = 1', 'y = 0')40hold off;414243%% =========== Part 1: Regularized Logistic Regression ============44% In this part, you are given a dataset with data points that are not45% linearly separable. However, you would still like to use logistic46% regression to classify the data points.47%48% To do so, you introduce more features to use -- in particular, you add49% polynomial features to our data matrix (similar to polynomial50% regression).51%5253% Add Polynomial Features5455% Note that mapFeature also adds a column of ones for us, so the intercept56% term is handled57X = mapFeature(X(:,1), X(:,2));5859% Initialize fitting parameters60initial_theta = zeros(size(X, 2), 1);6162% Set regularization parameter lambda to 163lambda = 1;6465% Compute and display initial cost and gradient for regularized logistic66% regression67[cost, grad] = costFunctionReg(initial_theta, X, y, lambda);6869fprintf('Cost at initial theta (zeros): %f\n', cost);70fprintf('Expected cost (approx): 0.693\n');71fprintf('Gradient at initial theta (zeros) - first five values only:\n');72fprintf(' %f \n', grad(1:5));73fprintf('Expected gradients (approx) - first five values only:\n');74fprintf(' 0.0085\n 0.0188\n 0.0001\n 0.0503\n 0.0115\n');7576fprintf('\nProgram paused. Press enter to continue.\n');77pause;7879% Compute and display cost and gradient80% with all-ones theta and lambda = 1081test_theta = ones(size(X,2),1);82[cost, grad] = costFunctionReg(test_theta, X, y, 10);8384fprintf('\nCost at test theta (with lambda = 10): %f\n', cost);85fprintf('Expected cost (approx): 3.16\n');86fprintf('Gradient at test theta - first five values only:\n');87fprintf(' %f \n', grad(1:5));88fprintf('Expected gradients (approx) - first five values only:\n');89fprintf(' 0.3460\n 0.1614\n 0.1948\n 0.2269\n 0.0922\n');9091fprintf('\nProgram paused. Press enter to continue.\n');92pause;9394%% ============= Part 2: Regularization and Accuracies =============95% Optional Exercise:96% In this part, you will get to try different values of lambda and97% see how regularization affects the decision coundart98%99% Try the following values of lambda (0, 1, 10, 100).100%101% How does the decision boundary change when you vary lambda? How does102% the training set accuracy vary?103%104105% Initialize fitting parameters106initial_theta = zeros(size(X, 2), 1);107108% Set regularization parameter lambda to 1 (you should vary this)109lambda = 1;110111% Set Options112options = optimset('GradObj', 'on', 'MaxIter', 400);113114% Optimize115[theta, J, exit_flag] = ...116fminunc(@(t)(costFunctionReg(t, X, y, lambda)), initial_theta, options);117118% Plot Boundary119plotDecisionBoundary(theta, X, y);120hold on;121title(sprintf('lambda = %g', lambda))122123% Labels and Legend124xlabel('Microchip Test 1')125ylabel('Microchip Test 2')126127legend('y = 1', 'y = 0', 'Decision boundary')128hold off;129130% Compute accuracy on our training set131p = predict(theta, X);132133fprintf('Train Accuracy: %f\n', mean(double(p == y)) * 100);134fprintf('Expected accuracy (with lambda = 1): 83.1 (approx)\n');135136137138