ex1_machine_learning
19 May 2016
In [1]:
%% Machine Learning Online Class
% Exercise 1: Linear regression with multiple variables
%
% Instructions
% ------------
%
% This file contains code that helps you get started on the
% linear regression exercise.
%
% You will need to complete the following functions in this
% exericse:
%
% warmUpExercise.m
% plotData.m
% gradientDescent.m
% computeCost.m
% gradientDescentMulti.m
% computeCostMulti.m
% featureNormalize.m
% normalEqn.m
%
% For this part of the exercise, you will need to change some
% parts of the code below for various experiments (e.g., changing
% learning rates).
%
In [3]:
%% Initialization
%% ================ Part 1: Feature Normalization ================
%% Clear and Close Figures
clear ; close all ; clc
fprintf ( 'Loading data ...\n' );
%% Load Data
data = load ( 'ex1data2.txt' );
X = data (:, 1 : 2 );
y = data (:, 3 );
m = length ( y );
% Print out some data points
fprintf ( 'First 10 examples from the dataset: \n' );
fprintf ( ' x = [%.0f %.0f], y = %.0f \n' , [ X ( 1 : 10 ,:) y ( 1 : 10 ,:)] ' );
% Scale features and set them to zero mean
fprintf ( 'Normalizing Features ...\n' );
[ X mu sigma ] = featureNormalize ( X );
% Add intercept term to X
X = [ ones ( m , 1 ) X ];
[H[2J
Loading data ...
First 10 examples from the dataset:
x = [2104 3], y = 399900
x = [1600 3], y = 329900
x = [2400 3], y = 369000
x = [1416 2], y = 232000
x = [3000 4], y = 539900
x = [1985 4], y = 299900
x = [1534 3], y = 314900
x = [1427 3], y = 198999
x = [1380 3], y = 212000
x = [1494 3], y = 242500
Normalizing Features ...
warning: operator -: automatic broadcasting operation applied
warning: quotient: automatic broadcasting operation applied
In [4]:
%% ================ Part 2: Gradient Descent ================
% ====================== YOUR CODE HERE ======================
% Instructions: We have provided you with the following starter
% code that runs gradient descent with a particular
% learning rate (alpha).
%
% Your task is to first make sure that your functions -
% computeCost and gradientDescent already work with
% this starter code and support multiple variables.
%
% After that, try running gradient descent with
% different values of alpha and see which one gives
% you the best result.
%
% Finally, you should complete the code at the end
% to predict the price of a 1650 sq-ft, 3 br house.
%
% Hint: By using the 'hold on' command, you can plot multiple
% graphs on the same figure.
%
% Hint: At prediction, make sure you do the same feature normalization.
%
In [5]:
fprintf ( 'Running gradient descent ...\n' );
% Choose some alpha value
alpha = 0.01 ;
num_iters = 400 ;
% Init Theta and Run Gradient Descent
theta = zeros ( 3 , 1 );
[ theta , J_history ] = gradientDescentMulti ( X , y , theta , alpha , num_iters );
% Plot the convergence graph
figure ;
plot ( 1 : numel ( J_history ), J_history , '-b' , 'LineWidth' , 2 );
xlabel ( 'Number of iterations' );
ylabel ( 'Cost J' );
% Display gradient descent's result
fprintf ( 'Theta computed from gradient descent: \n' );
fprintf ( ' %f \n' , theta );
fprintf ( '\n' );
% Estimate the price of a 1650 sq-ft, 3 br house
% ====================== YOUR CODE HERE ======================
% Recall that the first column of X is all-ones. Thus, it does
% not need to be normalized.
price = 0 ; % You should change this
price = theta ' * [ 1 ; 1650 ; 3 ];
% ============================================================
fprintf ([ 'Predicted price of a 1650 sq-ft, 3 br house ' ...
'(using gradient descent):\n $%f\n' ], price );
Running gradient descent ...
Theta computed from gradient descent:
334302.063993
100087.116006
3673.548451
Predicted price of a 1650 sq-ft, 3 br house (using gradient descent):
$165489064.118993
In [6]:
%% ================ Part 3: Normal Equations ================
fprintf ( 'Solving with normal equations...\n' );
% ====================== YOUR CODE HERE ======================
% Instructions: The following code computes the closed form
% solution for linear regression using the normal
% equations. You should complete the code in
% normalEqn.m
%
% After doing so, you should complete this code
% to predict the price of a 1650 sq-ft, 3 br house.
%
Solving with normal equations...
In [7]:
%% Load Data
data = csvread ( 'ex1data2.txt' );
X = data (:, 1 : 2 );
y = data (:, 3 );
m = length ( y );
% Add intercept term to X
X = [ ones ( m , 1 ) X ];
% Calculate the parameters from the normal equation
theta = normalEqn ( X , y );
% Display normal equation's result
fprintf ( 'Theta computed from the normal equations: \n' );
fprintf ( ' %f \n' , theta );
fprintf ( '\n' );
% Estimate the price of a 1650 sq-ft, 3 br house
% ====================== YOUR CODE HERE ======================
price = 0 ; % You should change this
price = theta ' * [ 1 ; 1650 ; 3 ];
% ============================================================
fprintf ([ 'Predicted price of a 1650 sq-ft, 3 br house ' ...
'(using normal equations):\n $%f\n' ], price );
Theta computed from the normal equations:
89597.909542
139.210674
-8738.019112
Predicted price of a 1650 sq-ft, 3 br house (using normal equations):
$293081.464335
In [None]: