|
| 1 | +%% Machine Learning Online Class |
| 2 | +% Exercise 6 | Support Vector Machines |
| 3 | +% |
| 4 | +% Instructions |
| 5 | +% ------------ |
| 6 | +% |
| 7 | +% This file contains code that helps you get started on the |
| 8 | +% exercise. You will need to complete the following functions: |
| 9 | +% |
| 10 | +% gaussianKernel.m |
| 11 | +% dataset3Params.m |
| 12 | +% processEmail.m |
| 13 | +% emailFeatures.m |
| 14 | +% |
| 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 | +% |
| 18 | + |
| 19 | +%% Initialization |
| 20 | +clear ; close all; clc |
| 21 | + |
| 22 | +%% =============== Part 1: Loading and Visualizing Data ================ |
| 23 | +% We start the exercise by first loading and visualizing the dataset. |
| 24 | +% The following code will load the dataset into your environment and plot |
| 25 | +% the data. |
| 26 | +% |
| 27 | + |
| 28 | +fprintf('Loading and Visualizing Data ...\n') |
| 29 | + |
| 30 | +% Load from ex6data1: |
| 31 | +% You will have X, y in your environment |
| 32 | +load('ex6data1.mat'); |
| 33 | + |
| 34 | +% Plot training data |
| 35 | +plotData(X, y); |
| 36 | + |
| 37 | +fprintf('Program paused. Press enter to continue.\n'); |
| 38 | +pause; |
| 39 | + |
| 40 | +%% ==================== Part 2: Training Linear SVM ==================== |
| 41 | +% The following code will train a linear SVM on the dataset and plot the |
| 42 | +% decision boundary learned. |
| 43 | +% |
| 44 | + |
| 45 | +% Load from ex6data1: |
| 46 | +% You will have X, y in your environment |
| 47 | +load('ex6data1.mat'); |
| 48 | + |
| 49 | +fprintf('\nTraining Linear SVM ...\n') |
| 50 | + |
| 51 | +% You should try to change the C value below and see how the decision |
| 52 | +% boundary varies (e.g., try C = 1000) |
| 53 | +C = 1; |
| 54 | +model = svmTrain(X, y, C, @linearKernel, 1e-3, 20); |
| 55 | +visualizeBoundaryLinear(X, y, model); |
| 56 | + |
| 57 | +fprintf('Program paused. Press enter to continue.\n'); |
| 58 | +pause; |
| 59 | + |
| 60 | +%% =============== Part 3: Implementing Gaussian Kernel =============== |
| 61 | +% You will now implement the Gaussian kernel to use |
| 62 | +% with the SVM. You should complete the code in gaussianKernel.m |
| 63 | +% |
| 64 | +fprintf('\nEvaluating the Gaussian Kernel ...\n') |
| 65 | + |
| 66 | +x1 = [1 2 1]; x2 = [0 4 -1]; sigma = 2; |
| 67 | +sim = gaussianKernel(x1, x2, sigma); |
| 68 | + |
| 69 | +fprintf(['Gaussian Kernel between x1 = [1; 2; 1], x2 = [0; 4; -1], sigma = %f :' ... |
| 70 | + '\n\t%f\n(for sigma = 2, this value should be about 0.324652)\n'], sigma, sim); |
| 71 | + |
| 72 | +fprintf('Program paused. Press enter to continue.\n'); |
| 73 | +pause; |
| 74 | + |
| 75 | +%% =============== Part 4: Visualizing Dataset 2 ================ |
| 76 | +% The following code will load the next dataset into your environment and |
| 77 | +% plot the data. |
| 78 | +% |
| 79 | + |
| 80 | +fprintf('Loading and Visualizing Data ...\n') |
| 81 | + |
| 82 | +% Load from ex6data2: |
| 83 | +% You will have X, y in your environment |
| 84 | +load('ex6data2.mat'); |
| 85 | + |
| 86 | +% Plot training data |
| 87 | +plotData(X, y); |
| 88 | + |
| 89 | +fprintf('Program paused. Press enter to continue.\n'); |
| 90 | +pause; |
| 91 | + |
| 92 | +%% ========== Part 5: Training SVM with RBF Kernel (Dataset 2) ========== |
| 93 | +% After you have implemented the kernel, we can now use it to train the |
| 94 | +% SVM classifier. |
| 95 | +% |
| 96 | +fprintf('\nTraining SVM with RBF Kernel (this may take 1 to 2 minutes) ...\n'); |
| 97 | + |
| 98 | +% Load from ex6data2: |
| 99 | +% You will have X, y in your environment |
| 100 | +load('ex6data2.mat'); |
| 101 | + |
| 102 | +% SVM Parameters |
| 103 | +C = 1; sigma = 0.1; |
| 104 | + |
| 105 | +% We set the tolerance and max_passes lower here so that the code will run |
| 106 | +% faster. However, in practice, you will want to run the training to |
| 107 | +% convergence. |
| 108 | +model= svmTrain(X, y, C, @(x1, x2) gaussianKernel(x1, x2, sigma)); |
| 109 | +visualizeBoundary(X, y, model); |
| 110 | + |
| 111 | +fprintf('Program paused. Press enter to continue.\n'); |
| 112 | +pause; |
| 113 | + |
| 114 | +%% =============== Part 6: Visualizing Dataset 3 ================ |
| 115 | +% The following code will load the next dataset into your environment and |
| 116 | +% plot the data. |
| 117 | +% |
| 118 | + |
| 119 | +fprintf('Loading and Visualizing Data ...\n') |
| 120 | + |
| 121 | +% Load from ex6data3: |
| 122 | +% You will have X, y in your environment |
| 123 | +load('ex6data3.mat'); |
| 124 | + |
| 125 | +% Plot training data |
| 126 | +plotData(X, y); |
| 127 | + |
| 128 | +fprintf('Program paused. Press enter to continue.\n'); |
| 129 | +pause; |
| 130 | + |
| 131 | +%% ========== Part 7: Training SVM with RBF Kernel (Dataset 3) ========== |
| 132 | + |
| 133 | +% This is a different dataset that you can use to experiment with. Try |
| 134 | +% different values of C and sigma here. |
| 135 | +% |
| 136 | + |
| 137 | +% Load from ex6data3: |
| 138 | +% You will have X, y in your environment |
| 139 | +load('ex6data3.mat'); |
| 140 | + |
| 141 | +% Try different SVM Parameters here |
| 142 | +[C, sigma] = dataset3Params(X, y, Xval, yval); |
| 143 | + |
| 144 | +% Train the SVM |
| 145 | +model= svmTrain(X, y, C, @(x1, x2) gaussianKernel(x1, x2, sigma)); |
| 146 | +visualizeBoundary(X, y, model); |
| 147 | + |
| 148 | +fprintf('Program paused. Press enter to continue.\n'); |
| 149 | +pause; |
| 150 | + |
0 commit comments