ex6.m 4.2 KB

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