38 lines
994 B
Matlab
38 lines
994 B
Matlab
%% This loads our data
|
|
[X, y] = load_data_ex2();
|
|
y
|
|
|
|
%% Normalise and initialize.
|
|
[X, mean_vec, std_vec] = normalise_features(X);
|
|
|
|
%after normalising we add the bias
|
|
X = [ones(size(X, 1), 1), X];
|
|
|
|
%initialise theta
|
|
theta = [0.0, 0.0, 0.0];
|
|
% Learning rate
|
|
alpha = 0.2;
|
|
% Number of iterations for calculating theta values in gradient descent
|
|
iterations = 100;
|
|
|
|
% Perform gradient descent on data to calculate the values for theta that make
|
|
% the best hypothesis function.
|
|
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;
|