ex2.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 logistic
  7. % regression exercise. You will need to complete the following functions
  8. % in this exericse:
  9. %
  10. % sigmoid.m
  11. % costFunction.m
  12. % predict.m
  13. % costFunctionReg.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. %% Load Data
  21. % The first two columns contains the exam scores and the third column
  22. % contains the label.
  23. data = load('ex2data1.txt');
  24. X = data(:, [1, 2]); y = data(:, 3);
  25. %% ==================== Part 1: Plotting ====================
  26. % We start the exercise by first plotting the data to understand the
  27. % the problem we are working with.
  28. fprintf(['Plotting data with + indicating (y = 1) examples and o ' ...
  29. 'indicating (y = 0) examples.\n']);
  30. plotData(X, y);
  31. % Put some labels
  32. hold on;
  33. % Labels and Legend
  34. xlabel('Exam 1 score')
  35. ylabel('Exam 2 score')
  36. % Specified in plot order
  37. legend('Admitted', 'Not admitted')
  38. hold off;
  39. fprintf('\nProgram paused. Press enter to continue.\n');
  40. pause;
  41. %% ============ Part 2: Compute Cost and Gradient ============
  42. % In this part of the exercise, you will implement the cost and gradient
  43. % for logistic regression. You neeed to complete the code in
  44. % costFunction.m
  45. % Setup the data matrix appropriately, and add ones for the intercept term
  46. [m, n] = size(X);
  47. % Add intercept term to x and X_test
  48. X = [ones(m, 1) X];
  49. % Initialize fitting parameters
  50. initial_theta = zeros(n + 1, 1);
  51. % Compute and display initial cost and gradient
  52. [cost, grad] = costFunction(initial_theta, X, y);
  53. fprintf('Cost at initial theta (zeros): %f\n', cost);
  54. fprintf('Expected cost (approx): 0.693\n');
  55. fprintf('Gradient at initial theta (zeros): \n');
  56. fprintf(' %f \n', grad);
  57. fprintf('Expected gradients (approx):\n -0.1000\n -12.0092\n -11.2628\n');
  58. % Compute and display cost and gradient with non-zero theta
  59. test_theta = [-24; 0.2; 0.2];
  60. [cost, grad] = costFunction(test_theta, X, y);
  61. fprintf('\nCost at test theta: %f\n', cost);
  62. fprintf('Expected cost (approx): 0.218\n');
  63. fprintf('Gradient at test theta: \n');
  64. fprintf(' %f \n', grad);
  65. fprintf('Expected gradients (approx):\n 0.043\n 2.566\n 2.647\n');
  66. fprintf('\nProgram paused. Press enter to continue.\n');
  67. pause;
  68. %% ============= Part 3: Optimizing using fminunc =============
  69. % In this exercise, you will use a built-in function (fminunc) to find the
  70. % optimal parameters theta.
  71. % Set options for fminunc
  72. options = optimset('GradObj', 'on', 'MaxIter', 400);
  73. % Run fminunc to obtain the optimal theta
  74. % This function will return theta and the cost
  75. [theta, cost] = ...
  76. fminunc(@(t)(costFunction(t, X, y)), initial_theta, options);
  77. % Print theta to screen
  78. fprintf('Cost at theta found by fminunc: %f\n', cost);
  79. fprintf('Expected cost (approx): 0.203\n');
  80. fprintf('theta: \n');
  81. fprintf(' %f \n', theta);
  82. fprintf('Expected theta (approx):\n');
  83. fprintf(' -25.161\n 0.206\n 0.201\n');
  84. % Plot Boundary
  85. plotDecisionBoundary(theta, X, y);
  86. % Put some labels
  87. hold on;
  88. % Labels and Legend
  89. xlabel('Exam 1 score')
  90. ylabel('Exam 2 score')
  91. % Specified in plot order
  92. legend('Admitted', 'Not admitted')
  93. hold off;
  94. fprintf('\nProgram paused. Press enter to continue.\n');
  95. pause;
  96. %% ============== Part 4: Predict and Accuracies ==============
  97. % After learning the parameters, you'll like to use it to predict the outcomes
  98. % on unseen data. In this part, you will use the logistic regression model
  99. % to predict the probability that a student with score 45 on exam 1 and
  100. % score 85 on exam 2 will be admitted.
  101. %
  102. % Furthermore, you will compute the training and test set accuracies of
  103. % our model.
  104. %
  105. % Your task is to complete the code in predict.m
  106. % Predict probability for a student with score 45 on exam 1
  107. % and score 85 on exam 2
  108. prob = sigmoid([1 45 85] * theta);
  109. fprintf(['For a student with scores 45 and 85, we predict an admission ' ...
  110. 'probability of %f\n'], prob);
  111. fprintf('Expected value: 0.775 +/- 0.002\n\n');
  112. % Compute accuracy on our training set
  113. p = predict(theta, X);
  114. fprintf('Train Accuracy: %f\n', mean(double(p == y)) * 100);
  115. fprintf('Expected accuracy (approx): 89.0\n');
  116. fprintf('\n');