Generalized linear regression
This commit is contained in:
@@ -1,9 +1,9 @@
|
|||||||
function hypothesis = calculate_hypothesis(X, theta, training_example)
|
function hypothesis = calculate_hypothesis(X, theta, training_example)
|
||||||
%CALCULATE_HYPOTHESIS This calculates the hypothesis for a given X,
|
%CALCULATE_HYPOTHESIS This calculates the hypothesis for a given X,
|
||||||
%theta and specified training example
|
%theta and specified training example
|
||||||
x0 = X(training_example, 1);
|
x = X(training_example, 1:length(theta));
|
||||||
x1 = X(training_example, 2);
|
|
||||||
|
|
||||||
hypothesis = theta(1)*x0+theta(2)*x1;
|
hypothesis = sum(x.*theta);
|
||||||
|
%hypothesis = theta(1)*x0+theta(2)*x1+theta(3)*x2;
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
+12
-23
@@ -19,34 +19,23 @@ function theta = gradient_descent(X, y, theta, alpha, iterations, do_plot)
|
|||||||
% gradient descent
|
% gradient descent
|
||||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
|
|
||||||
theta_0 = theta(1);
|
% fill temporary array with values of theta
|
||||||
theta_1 = theta(2);
|
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
|
for i = 1:m
|
||||||
sigma = 0.0;
|
hypothesis = calculate_hypothesis(X, theta, i);
|
||||||
|
output = y(i);
|
||||||
|
sigma = sigma + (hypothesis - output) * X(i, ind);
|
||||||
|
end
|
||||||
|
|
||||||
for i = 1:m
|
theta_temp(ind) = theta_temp(ind) - ((alpha * 1.0) / m) * sigma;
|
||||||
hypothesis = calculate_hypothesis(X, theta, i);
|
|
||||||
output = y(i);
|
|
||||||
sigma = sigma + (hypothesis - output);
|
|
||||||
end
|
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
|
%update theta
|
||||||
theta = [theta_0, theta_1];
|
theta = theta_temp;
|
||||||
|
|
||||||
%update cost_vector
|
%update cost_vector
|
||||||
cost_vector = [cost_vector; compute_cost(X, y, theta)];
|
cost_vector = [cost_vector; compute_cost(X, y, theta)];
|
||||||
|
|||||||
@@ -4,11 +4,11 @@
|
|||||||
%% initialize
|
%% initialize
|
||||||
theta = [0.0, 0.0]; %The weights of our model.
|
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;
|
iterations = 50;
|
||||||
|
|
||||||
%do plotting
|
%do plotting
|
||||||
do_plot = true;
|
do_plot = true;
|
||||||
|
|
||||||
%% run gradient descent
|
%% run gradient descent
|
||||||
t = gradient_descent(X, y, theta, alpha, iterations, do_plot);
|
t = gradient_descent(X, y, theta, alpha, iterations, do_plot);
|
||||||
|
|||||||
Reference in New Issue
Block a user