myex2.m 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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('Expected cost (approx): 0.693\n');
  57. fprintf('Gradient at initial theta (zeros) - first five values only:\n');
  58. fprintf(' %f \n', grad(1:5));
  59. fprintf('Expected gradients (approx) - first five values only:\n');
  60. fprintf(' 0.0085\n 0.0188\n 0.0001\n 0.0503\n 0.0115\n');
  61. fprintf('\nProgram paused. Press enter to continue.\n');
  62. % Compute and display cost and gradient
  63. % with all-ones theta and lambda = 10
  64. test_theta = ones(size(X,2),1);
  65. [cost, grad] = costFunctionReg(test_theta, X, y, 10);
  66. fprintf('\nCost at test theta (with lambda = 10): %f\n', cost);
  67. fprintf('Expected cost (approx): 3.16\n');
  68. fprintf('Gradient at test theta - first five values only:\n');
  69. fprintf(' %f \n', grad(1:5));
  70. fprintf('Expected gradients (approx) - first five values only:\n');
  71. fprintf(' 0.3460\n 0.1614\n 0.1948\n 0.2269\n 0.0922\n');
  72. fprintf('\nProgram paused. Press enter to continue.\n');