In [21]:
% Initial Clean-up
clear; close all; clc

In [22]:
fprintf('Loading Data...\n')


Loading Data...

In [23]:
%% Load Data:
data = load('./data/ex1data2.txt');

In [24]:
size(data)


ans =

   47    3


In [25]:
X = data(:, 1:2);
y = data(:, 3);
m = length(y);

In [26]:
length(y)


ans =  47

In [27]:
fprintf('First 10 examples from the dataset: \n');
fprintf('x = [%.0f %.0f], y = %.0f \n', [X(1:10, :) y(1:10, :)])


First 10 examples from the dataset: 
x = [2104 1600], y = 2400 
x = [1416 3000], y = 1985 
x = [1534 1427], y = 1380 
x = [1494 3], y = 3 
x = [3 2], y = 4 
x = [4 3], y = 3 
x = [3 3], y = 399900 
x = [329900 369000], y = 232000 
x = [539900 299900], y = 314900 
x = [198999 212000], y = 242500 

In [28]:
fprintf('Program paused. Press enter to continue.\n');


Program paused. Press enter to continue.

In [29]:
fprintf('Normalizing Features ...\n');

[X mu sigma] = featureNormalize(X);

% Add intercept term to X
X = [ones(m, 1) X];


Normalizing Features ...
mu =

   2000.6809      3.1702

sigma =

   794.70235     0.76098


In [30]:
fprintf('Running gradient descent ...\n');

% Learning Rate:
alpha = 0.01;
num_iter = 400;


Running gradient descent ...

In [32]:
% Initialize Theta and Run Gradient Descent
theta = zeros(3,1);

[theta, J_history] = gradientDescentMulti(X,y, theta, alpha, num_iter);

In [33]:
% Plotting the convergence graph

figure; 
plot(1:numel(J_history), J_history, '-b', 'LineWidth', 2);
xlabel('Number of iterations');
ylabel('Cost J');


Gnuplot Produced by GNUPLOT 5.0 patchlevel 5 0 1e+10 2e+10 3e+10 4e+10 5e+10 6e+10 7e+10 0 50 100 150 200 250 300 350 400 Cost J Number of iterations gnuplot_plot_1a

In [34]:
% Display gradient descent's result
fprintf('Theta computed from gradient descent: \n');
fprintf(' %f \n', theta);
fprintf('\n');


Theta computed from gradient descent: 
 334302.063993 
 100087.116006 
 3673.548451 


In [35]:
% Let's estimate a new one
% Estimate the price of a 1650 sq-ft, 3 br house

% Add it to the vector
d = [1650 3];
% normalize it 
d = (d - mu) ./ sigma;
d = [ones(1,1), d];

% Estimating price using the minimized theta
price = d * theta;

In [48]:
fprintf(['Predicted price of a 1650 sq-ft, 3 br house (using gradient descent):\n $%.3f\n'], price);


Predicted price of a 1650 sq-ft, 3 br house (using gradient descent):
 $293499.096

Solving it with Normal Equations:


In [37]:
fprintf('Solving with normal equations...\n');


Solving with normal equations...

In [39]:
%% Load Data
data = csvread('./data/ex1data2.txt');
X = data(:, 1:2);
y = data(:, 3);
m = length(y);

In [40]:
% Add intercept term to X
X = [ones(m, 1) X];

In [41]:
% Calculate the parameters from the normal equations
theta = normalEqn(X, y);

In [42]:
fprintf('Theta computed from the normal equations: \n');
fprintf('%f \n', theta);
fprintf('\n');


Theta computed from the normal equations: 
89597.909544 
139.210674 
-8738.019113 


In [44]:
d = [1 1653 3];
price = d * theta ;

In [46]:
fprintf(['Predicted price of a 1650 sq-ft, 3 br house (using normal equations):\n $%.3f\n'], price);


Predicted price of a 1650 sq-ft, 3 br house (using normal equations):
 $293499.096

In [ ]: