From 1bc2e6c72fa6d81b6fa67cb95402f0cc84ba6eb4 Mon Sep 17 00:00:00 2001 From: Sam Perry Date: Thu, 3 Nov 2016 14:09:27 +0000 Subject: [PATCH] Completed task 2 --- compute_cost.m | 2 +- mllab1.m | 2 +- mllab2.m | 20 ++++++++++++++++++-- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/compute_cost.m b/compute_cost.m index 5073c25..dc17a2b 100644 --- a/compute_cost.m +++ b/compute_cost.m @@ -14,4 +14,4 @@ function J = compute_cost(X, y, theta) end J = J * (1.0 / (2 * m)); -end \ No newline at end of file +end diff --git a/mllab1.m b/mllab1.m index 4b93692..b2c5608 100644 --- a/mllab1.m +++ b/mllab1.m @@ -11,4 +11,4 @@ iterations = 50; do_plot = true; %% run gradient descent -t = gradient_descent(X, y, theta, alpha, iterations, do_plot); +t = gradient_descent(X, y, theta, alpha, iterations, do_plot) diff --git a/mllab2.m b/mllab2.m index 4bdff0b..94f5de4 100644 --- a/mllab2.m +++ b/mllab2.m @@ -1,5 +1,6 @@ %% This loads our data [X, y] = load_data_ex2(); +y %% Normalise and initialize. [X, mean_vec, std_vec] = normalise_features(X); @@ -9,10 +10,25 @@ X = [ones(size(X, 1), 1), X]; %initialise theta theta = [0.0, 0.0, 0.0]; -alpha = 0.1; +alpha = 0.2; iterations = 100; %% -t = gradient_descent(X, y, theta, alpha, iterations); +t = gradient_descent(X, y, theta, alpha, iterations, false) + +% Declare new pair of input features with which to calculate a predicted output +% value. +input_size = 3000.0; +input_bedrooms = 4.0; + +% Normalize features +is_norm = (input_size - mean_vec(1)) / std_vec(1) +ib_norm = (input_bedrooms - mean_vec(2)) / std_vec(2) + +% Use values for theta generated by gradient descent to predict the output +prediction = t(1) + t(2)*is_norm + t(3)*ib_norm; +% Round and print output +round(prediction) + disp 'Press enter to exit!'; pause;