You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ex2_reg.m 3.0 kB

8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. %% Machine Learning Online Class - Exercise 2: Logistic Regression
  2. %
  3. % Instructions
  4. % ------------
  5. %
  6. % This file contains code that helps you get started on the second part
  7. % of the exercise which covers regularization with logistic regression.
  8. %
  9. % You will need to complete the following functions in this exericse:
  10. %
  11. % sigmoid.m
  12. % costFunction.m
  13. % predict.m
  14. % costFunctionReg.m
  15. %
  16. % For this exercise, you will not need to change any code in this file,
  17. % or any other files other than those mentioned above.
  18. %
  19. %% Initialization
  20. clear ; close all; clc
  21. %% Load Data
  22. % The first two columns contains the X values and the third column
  23. % contains the label (y).
  24. data = load('ex2data2.txt');
  25. X = data(:, [1, 2]); y = data(:, 3);
  26. plotData(X, y);
  27. % Put some labels
  28. hold on;
  29. % Labels and Legend
  30. xlabel('Microchip Test 1')
  31. ylabel('Microchip Test 2')
  32. % Specified in plot order
  33. legend('y = 1', 'y = 0')
  34. hold off;
  35. %% =========== Part 1: Regularized Logistic Regression ============
  36. % In this part, you are given a dataset with data points that are not
  37. % linearly separable. However, you would still like to use logistic
  38. % regression to classify the data points.
  39. %
  40. % To do so, you introduce more features to use -- in particular, you add
  41. % polynomial features to our data matrix (similar to polynomial
  42. % regression).
  43. %
  44. % Add Polynomial Features
  45. % Note that mapFeature also adds a column of ones for us, so the intercept
  46. % term is handled
  47. X = mapFeature(X(:,1), X(:,2));
  48. % Initialize fitting parameters
  49. initial_theta = zeros(size(X, 2), 1);
  50. % Set regularization parameter lambda to 1
  51. lambda = 1;
  52. % Compute and display initial cost and gradient for regularized logistic
  53. % regression
  54. [cost, grad] = costFunctionReg(initial_theta, X, y, lambda);
  55. fprintf('Cost at initial theta (zeros): %f\n', cost);
  56. fprintf('\nProgram paused. Press enter to continue.\n');
  57. pause;
  58. %% ============= Part 2: Regularization and Accuracies =============
  59. % Optional Exercise:
  60. % In this part, you will get to try different values of lambda and
  61. % see how regularization affects the decision coundart
  62. %
  63. % Try the following values of lambda (0, 1, 10, 100).
  64. %
  65. % How does the decision boundary change when you vary lambda? How does
  66. % the training set accuracy vary?
  67. %
  68. % Initialize fitting parameters
  69. initial_theta = zeros(size(X, 2), 1);
  70. % Set regularization parameter lambda to 1 (you should vary this)
  71. lambda = 100;
  72. % Set Options
  73. options = optimset('GradObj', 'on', 'MaxIter', 400);
  74. % Optimize
  75. [theta, J, exit_flag] = ...
  76. fminunc(@(t)(costFunctionReg(t, X, y, 1)), initial_theta, options);
  77. % Plot Boundary
  78. plotDecisionBoundary(theta, X, y);
  79. hold on;
  80. title(sprintf('lambda = %g', lambda))
  81. % Labels and Legend
  82. xlabel('Microchip Test 1')
  83. ylabel('Microchip Test 2')
  84. legend('y = 1', 'y = 0', 'Decision boundary')
  85. hold off;
  86. % Compute accuracy on our training set
  87. p = predict(theta, X);
  88. fprintf('Train Accuracy: %f\n', mean(double(p == y)) * 100);

机器学习

Contributors (1)