Generalized linear regression

This commit is contained in:
Sam Perry
2016-10-19 11:01:34 +01:00
parent 676b75d0b6
commit 21b247a4e9
4 changed files with 18 additions and 29 deletions
+3 -3
View File
@@ -1,9 +1,9 @@
function hypothesis = calculate_hypothesis(X, theta, training_example)
%CALCULATE_HYPOTHESIS This calculates the hypothesis for a given X,
%theta and specified training example
x0 = X(training_example, 1);
x1 = X(training_example, 2);
x = X(training_example, 1:length(theta));
hypothesis = theta(1)*x0+theta(2)*x1;
hypothesis = sum(x.*theta);
%hypothesis = theta(1)*x0+theta(2)*x1+theta(3)*x2;
end
+12 -23
View File
@@ -19,34 +19,23 @@ function theta = gradient_descent(X, y, theta, alpha, iterations, do_plot)
% gradient descent
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
theta_0 = theta(1);
theta_1 = theta(2);
% fill temporary array with values of theta
theta_temp = theta;
for ind = 1:length(theta_temp)
%update theta(1) and store in temporary variable theta_0
sigma = 0.0;
%update theta(1) and store in temporary variable theta_0
sigma = 0.0;
for i = 1:m
hypothesis = calculate_hypothesis(X, theta, i);
output = y(i);
sigma = sigma + (hypothesis - output) * X(i, ind);
end
for i = 1:m
hypothesis = calculate_hypothesis(X, theta, i);
output = y(i);
sigma = sigma + (hypothesis - output);
theta_temp(ind) = theta_temp(ind) - ((alpha * 1.0) / m) * sigma;
end
theta_0 = theta_0 - ((alpha * 1.0) / m) * sigma;
%update theta(2) and store in temporary variable theta_1
sigma = 0.0;
for i = 1:m
hypothesis = calculate_hypothesis(X, theta, i);
output = y(i);
sigma = sigma + (hypothesis - output) * X(i, 2);
end
theta_1 = theta_1 - ((alpha * 1.0) / m) * sigma;
%update theta
theta = [theta_0, theta_1];
theta = theta_temp;
%update cost_vector
cost_vector = [cost_vector; compute_cost(X, y, theta)];
+1 -1
View File
@@ -4,7 +4,7 @@
%% initialize
theta = [0.0, 0.0]; %The weights of our model.
alpha = 0.01; %The step size for gradient descent.
alpha = 0.001; %The step size for gradient descent.
iterations = 50;
%do plotting