|
| 1 | +%% Machine Learning Online Class |
| 2 | +% Exercise 1: Linear regression with multiple variables |
| 3 | +% |
| 4 | +% Instructions |
| 5 | +% ------------ |
| 6 | +% |
| 7 | +% This file contains code that helps you get started on the |
| 8 | +% linear regression exercise. |
| 9 | +% |
| 10 | +% You will need to complete the following functions in this |
| 11 | +% exericse: |
| 12 | +% |
| 13 | +% warmUpExercise.m |
| 14 | +% plotData.m |
| 15 | +% gradientDescent.m |
| 16 | +% computeCost.m |
| 17 | +% gradientDescentMulti.m |
| 18 | +% computeCostMulti.m |
| 19 | +% featureNormalize.m |
| 20 | +% normalEqn.m |
| 21 | +% |
| 22 | +% For this part of the exercise, you will need to change some |
| 23 | +% parts of the code below for various experiments (e.g., changing |
| 24 | +% learning rates). |
| 25 | +% |
| 26 | + |
| 27 | +%% Initialization |
| 28 | + |
| 29 | +%% ================ Part 1: Feature Normalization ================ |
| 30 | + |
| 31 | +%% Clear and Close Figures |
| 32 | +clear ; close all; clc |
| 33 | + |
| 34 | +fprintf('Loading data ...\n'); |
| 35 | + |
| 36 | +%% Load Data |
| 37 | +data = load('ex1data2.txt'); |
| 38 | +X = data(:, 1:2); |
| 39 | +y = data(:, 3); |
| 40 | +m = length(y); |
| 41 | + |
| 42 | +% Print out some data points |
| 43 | +fprintf('First 10 examples from the dataset: \n'); |
| 44 | +fprintf(' x = [%.0f %.0f], y = %.0f \n', [X(1:10,:) y(1:10,:)]'); |
| 45 | + |
| 46 | +fprintf('Program paused. Press enter to continue.\n'); |
| 47 | +pause; |
| 48 | + |
| 49 | +% Scale features and set them to zero mean |
| 50 | +fprintf('Normalizing Features ...\n'); |
| 51 | + |
| 52 | +[X mu sigma] = featureNormalize(X); |
| 53 | + |
| 54 | +% Add intercept term to X |
| 55 | +X = [ones(m, 1) X]; |
| 56 | + |
| 57 | + |
| 58 | +%% ================ Part 2: Gradient Descent ================ |
| 59 | + |
| 60 | +% ====================== YOUR CODE HERE ====================== |
| 61 | +% Instructions: We have provided you with the following starter |
| 62 | +% code that runs gradient descent with a particular |
| 63 | +% learning rate (alpha). |
| 64 | +% |
| 65 | +% Your task is to first make sure that your functions - |
| 66 | +% computeCost and gradientDescent already work with |
| 67 | +% this starter code and support multiple variables. |
| 68 | +% |
| 69 | +% After that, try running gradient descent with |
| 70 | +% different values of alpha and see which one gives |
| 71 | +% you the best result. |
| 72 | +% |
| 73 | +% Finally, you should complete the code at the end |
| 74 | +% to predict the price of a 1650 sq-ft, 3 br house. |
| 75 | +% |
| 76 | +% Hint: By using the 'hold on' command, you can plot multiple |
| 77 | +% graphs on the same figure. |
| 78 | +% |
| 79 | +% Hint: At prediction, make sure you do the same feature normalization. |
| 80 | +% |
| 81 | + |
| 82 | +fprintf('Running gradient descent ...\n'); |
| 83 | + |
| 84 | +% Choose some alpha value |
| 85 | +alpha = 0.01; |
| 86 | +num_iters = 400; |
| 87 | + |
| 88 | +% Init Theta and Run Gradient Descent |
| 89 | +theta = zeros(3, 1); |
| 90 | +[theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters); |
| 91 | + |
| 92 | +% Plot the convergence graph |
| 93 | +figure; |
| 94 | +plot(1:numel(J_history), J_history, '-b', 'LineWidth', 2); |
| 95 | +xlabel('Number of iterations'); |
| 96 | +ylabel('Cost J'); |
| 97 | + |
| 98 | +% Display gradient descent's result |
| 99 | +fprintf('Theta computed from gradient descent: \n'); |
| 100 | +fprintf(' %f \n', theta); |
| 101 | +fprintf('\n'); |
| 102 | + |
| 103 | +% Estimate the price of a 1650 sq-ft, 3 br house |
| 104 | +% ====================== YOUR CODE HERE ====================== |
| 105 | +% Recall that the first column of X is all-ones. Thus, it does |
| 106 | +% not need to be normalized. |
| 107 | +price = 0; % You should change this |
| 108 | + |
| 109 | + |
| 110 | +% ============================================================ |
| 111 | + |
| 112 | +fprintf(['Predicted price of a 1650 sq-ft, 3 br house ' ... |
| 113 | + '(using gradient descent):\n $%f\n'], price); |
| 114 | + |
| 115 | +fprintf('Program paused. Press enter to continue.\n'); |
| 116 | +pause; |
| 117 | + |
| 118 | +%% ================ Part 3: Normal Equations ================ |
| 119 | + |
| 120 | +fprintf('Solving with normal equations...\n'); |
| 121 | + |
| 122 | +% ====================== YOUR CODE HERE ====================== |
| 123 | +% Instructions: The following code computes the closed form |
| 124 | +% solution for linear regression using the normal |
| 125 | +% equations. You should complete the code in |
| 126 | +% normalEqn.m |
| 127 | +% |
| 128 | +% After doing so, you should complete this code |
| 129 | +% to predict the price of a 1650 sq-ft, 3 br house. |
| 130 | +% |
| 131 | + |
| 132 | +%% Load Data |
| 133 | +data = csvread('ex1data2.txt'); |
| 134 | +X = data(:, 1:2); |
| 135 | +y = data(:, 3); |
| 136 | +m = length(y); |
| 137 | + |
| 138 | +% Add intercept term to X |
| 139 | +X = [ones(m, 1) X]; |
| 140 | + |
| 141 | +% Calculate the parameters from the normal equation |
| 142 | +theta = normalEqn(X, y); |
| 143 | + |
| 144 | +% Display normal equation's result |
| 145 | +fprintf('Theta computed from the normal equations: \n'); |
| 146 | +fprintf(' %f \n', theta); |
| 147 | +fprintf('\n'); |
| 148 | + |
| 149 | + |
| 150 | +% Estimate the price of a 1650 sq-ft, 3 br house |
| 151 | +% ====================== YOUR CODE HERE ====================== |
| 152 | +price = 0; % You should change this |
| 153 | + |
| 154 | + |
| 155 | +% ============================================================ |
| 156 | + |
| 157 | +fprintf(['Predicted price of a 1650 sq-ft, 3 br house ' ... |
| 158 | + '(using normal equations):\n $%f\n'], price); |
| 159 | + |
0 commit comments