|
| 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 | +num_iters = 200; |
| 86 | + |
| 87 | +% Init Theta and Run Gradient Descent |
| 88 | +theta = zeros(3, 1); |
| 89 | +alpha = 0.1; |
| 90 | +[theta, J_history_1] = gradientDescentMulti(X, y, theta, alpha, num_iters); |
| 91 | +alpha = 0.3; |
| 92 | +theta = zeros(3, 1); |
| 93 | +[theta, J_history_2] = gradientDescentMulti(X, y, theta, alpha, num_iters); |
| 94 | +alpha = 1; |
| 95 | +theta = zeros(3, 1); |
| 96 | +[theta, J_history_3] = gradientDescentMulti(X, y, theta, alpha, num_iters); |
| 97 | + |
| 98 | +% Plot the convergence graph |
| 99 | +figure; |
| 100 | +plot(1:numel(J_history_1), J_history_1, '-b', 'LineWidth', 2); |
| 101 | +hold on; |
| 102 | +plot(1:numel(J_history_2), J_history_2, '-r', 'LineWidth', 2); |
| 103 | +plot(1:numel(J_history_3), J_history_3, '-k', 'LineWidth', 2); |
| 104 | +xlabel('Number of iterations'); |
| 105 | +ylabel('Cost J'); |
| 106 | + |
| 107 | +% Display gradient descent's result |
| 108 | +fprintf('Theta computed from gradient descent: \n'); |
| 109 | +fprintf(' %f \n', theta); |
| 110 | +fprintf('\n'); |
| 111 | + |
| 112 | +% Estimate the price of a 1650 sq-ft, 3 br house |
| 113 | +% ====================== YOUR CODE HERE ====================== |
| 114 | +% Recall that the first column of X is all-ones. Thus, it does |
| 115 | +% not need to be normalized. |
| 116 | +predictedHouseFeatures = [1650, 3]; |
| 117 | +%normalise |
| 118 | +predictedHouseFeatures = (predictedHouseFeatures - mu) ./ sigma; |
| 119 | +%add ones |
| 120 | +predictedHouseFeatures = [1,predictedHouseFeatures]; |
| 121 | +%predict |
| 122 | +price = predictedHouseFeatures*theta; % You should change this |
| 123 | + |
| 124 | + |
| 125 | +% ============================================================ |
| 126 | + |
| 127 | +fprintf(['Predicted price of a 1650 sq-ft, 3 br house ' ... |
| 128 | + '(using gradient descent):\n $%f\n'], price); |
| 129 | + |
| 130 | +fprintf('Program paused. Press enter to continue.\n'); |
| 131 | +pause; |
| 132 | + |
| 133 | +%% ================ Part 3: Normal Equations ================ |
| 134 | + |
| 135 | +fprintf('Solving with normal equations...\n'); |
| 136 | + |
| 137 | +% ====================== YOUR CODE HERE ====================== |
| 138 | +% Instructions: The following code computes the closed form |
| 139 | +% solution for linear regression using the normal |
| 140 | +% equations. You should complete the code in |
| 141 | +% normalEqn.m |
| 142 | +% |
| 143 | +% After doing so, you should complete this code |
| 144 | +% to predict the price of a 1650 sq-ft, 3 br house. |
| 145 | +% |
| 146 | + |
| 147 | +%% Load Data |
| 148 | +data = csvread('ex1data2.txt'); |
| 149 | +X = data(:, 1:2); |
| 150 | +y = data(:, 3); |
| 151 | +m = length(y); |
| 152 | + |
| 153 | +% Add intercept term to X |
| 154 | +X = [ones(m, 1) X]; |
| 155 | + |
| 156 | +% Calculate the parameters from the normal equation |
| 157 | +theta = normalEqn(X, y); |
| 158 | + |
| 159 | +% Display normal equation's result |
| 160 | +fprintf('Theta computed from the normal equations: \n'); |
| 161 | +fprintf(' %f \n', theta); |
| 162 | +fprintf('\n'); |
| 163 | + |
| 164 | + |
| 165 | +% Estimate the price of a 1650 sq-ft, 3 br house |
| 166 | +% ====================== YOUR CODE HERE ====================== |
| 167 | +price = 0; % You should change this |
| 168 | + |
| 169 | + |
| 170 | +% ============================================================ |
| 171 | +predictedHouseFeatures = [1650, 3]; |
| 172 | +predictedHouseFeatures = [1,predictedHouseFeatures]; |
| 173 | +%predict |
| 174 | +price = predictedHouseFeatures*theta; % You should change this |
| 175 | +fprintf(['Predicted price of a 1650 sq-ft, 3 br house ' ... |
| 176 | + '(using normal equations):\n $%f\n'], price); |
| 177 | + |
0 commit comments