Path: blob/master/Week 7/Programming Assignment - 6/ex6/ex6.m
863 views
%% Machine Learning Online Class1% Exercise 6 | Support Vector Machines2%3% Instructions4% ------------5%6% This file contains code that helps you get started on the7% exercise. You will need to complete the following functions:8%9% gaussianKernel.m10% dataset3Params.m11% processEmail.m12% emailFeatures.m13%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%1718%% Initialization19clear ; close all; clc2021%% =============== 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 plot24% the data.25%2627fprintf('Loading and Visualizing Data ...\n')2829% Load from ex6data1:30% You will have X, y in your environment31load('ex6data1.mat');3233% Plot training data34plotData(X, y);3536fprintf('Program paused. Press enter to continue.\n');37pause;3839%% ==================== Part 2: Training Linear SVM ====================40% The following code will train a linear SVM on the dataset and plot the41% decision boundary learned.42%4344% Load from ex6data1:45% You will have X, y in your environment46load('ex6data1.mat');4748fprintf('\nTraining Linear SVM ...\n')4950% You should try to change the C value below and see how the decision51% boundary varies (e.g., try C = 1000)52C = 1;53model = svmTrain(X, y, C, @linearKernel, 1e-3, 20);54visualizeBoundaryLinear(X, y, model);5556fprintf('Program paused. Press enter to continue.\n');57pause;5859%% =============== Part 3: Implementing Gaussian Kernel ===============60% You will now implement the Gaussian kernel to use61% with the SVM. You should complete the code in gaussianKernel.m62%63fprintf('\nEvaluating the Gaussian Kernel ...\n')6465x1 = [1 2 1]; x2 = [0 4 -1]; sigma = 2;66sim = gaussianKernel(x1, x2, sigma);6768fprintf(['Gaussian Kernel between x1 = [1; 2; 1], x2 = [0; 4; -1], sigma = %f :' ...69'\n\t%f\n(for sigma = 2, this value should be about 0.324652)\n'], sigma, sim);7071fprintf('Program paused. Press enter to continue.\n');72pause;7374%% =============== Part 4: Visualizing Dataset 2 ================75% The following code will load the next dataset into your environment and76% plot the data.77%7879fprintf('Loading and Visualizing Data ...\n')8081% Load from ex6data2:82% You will have X, y in your environment83load('ex6data2.mat');8485% Plot training data86plotData(X, y);8788fprintf('Program paused. Press enter to continue.\n');89pause;9091%% ========== Part 5: Training SVM with RBF Kernel (Dataset 2) ==========92% After you have implemented the kernel, we can now use it to train the93% SVM classifier.94%95fprintf('\nTraining SVM with RBF Kernel (this may take 1 to 2 minutes) ...\n');9697% Load from ex6data2:98% You will have X, y in your environment99load('ex6data2.mat');100101% SVM Parameters102C = 1; sigma = 0.1;103104% We set the tolerance and max_passes lower here so that the code will run105% faster. However, in practice, you will want to run the training to106% convergence.107model= svmTrain(X, y, C, @(x1, x2) gaussianKernel(x1, x2, sigma));108visualizeBoundary(X, y, model);109110fprintf('Program paused. Press enter to continue.\n');111pause;112113%% =============== Part 6: Visualizing Dataset 3 ================114% The following code will load the next dataset into your environment and115% plot the data.116%117118fprintf('Loading and Visualizing Data ...\n')119120% Load from ex6data3:121% You will have X, y in your environment122load('ex6data3.mat');123124% Plot training data125plotData(X, y);126127fprintf('Program paused. Press enter to continue.\n');128pause;129130%% ========== Part 7: Training SVM with RBF Kernel (Dataset 3) ==========131132% This is a different dataset that you can use to experiment with. Try133% different values of C and sigma here.134%135136% Load from ex6data3:137% You will have X, y in your environment138load('ex6data3.mat');139140% Try different SVM Parameters here141[C, sigma] = dataset3Params(X, y, Xval, yval);142143% Train the SVM144model= svmTrain(X, y, C, @(x1, x2) gaussianKernel(x1, x2, sigma));145visualizeBoundary(X, y, model);146147fprintf('Program paused. Press enter to continue.\n');148pause;149150151152