Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| aa8bd62519 |
@@ -1,9 +1,10 @@
|
||||
function hypothesis = calculate_hypothesis(X, theta, training_example)
|
||||
%CALCULATE_HYPOTHESIS This calculates the hypothesis for a given X,
|
||||
%theta and specified training example
|
||||
x = X(training_example, 1:length(theta));
|
||||
|
||||
hypothesis = sum(x.*theta);
|
||||
%hypothesis = theta(1)*x0+theta(2)*x1+theta(3)*x2;
|
||||
x = X(training_example, 1:2);
|
||||
|
||||
%hypothesis = sum(x.*theta);
|
||||
%
|
||||
hypothesis = theta(1)*x(1)+theta(2)*x(2)+theta(3)*x(2)^2+theta(4)*x(2)^3+theta(5)*x(2)^4+theta(6)*x(2)^5;
|
||||
end
|
||||
|
||||
|
||||
+9
-3
@@ -1,4 +1,4 @@
|
||||
function theta = gradient_descent(X, y, theta, alpha, iterations, do_plot)
|
||||
function theta = gradient_descent(X, y, theta, alpha, iterations, l, do_plot)
|
||||
%GRADIENT_DESCENT do Gradient Descent for a given X, y, theta, alpha
|
||||
%for a specified number of iterations
|
||||
|
||||
@@ -31,14 +31,20 @@ function theta = gradient_descent(X, y, theta, alpha, iterations, do_plot)
|
||||
sigma = sigma + (hypothesis - output) * X(i, ind);
|
||||
end
|
||||
|
||||
theta_temp(ind) = theta_temp(ind) - ((alpha * 1.0) / m) * sigma;
|
||||
% theta_temp(ind) = theta_temp(ind) - ((alpha * 1.0) / m) * sigma;
|
||||
if ind > 1
|
||||
theta_temp(ind) = (theta_temp(ind) * (1.0-alpha*(l/m))) - ((alpha * 1.0) / m) * sigma;
|
||||
else
|
||||
theta_temp(ind) = theta_temp(ind) - ((alpha * 1.0) / m) * sigma;
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
%update theta
|
||||
theta = theta_temp;
|
||||
|
||||
%update cost_vector
|
||||
cost_vector = [cost_vector; compute_cost(X, y, theta)];
|
||||
cost_vector = [cost_vector; compute_cost_regularised(X, y, theta, l)];
|
||||
|
||||
if do_plot
|
||||
plot_hypothesis(X, y, theta);
|
||||
|
||||
@@ -14,10 +14,10 @@ function mllab3()
|
||||
end
|
||||
|
||||
% initialise theta
|
||||
theta = [1.0, 1.0, 1.0, 1.0, 1.0];
|
||||
theta = [1.0, 1.0, 1.0, 1.0, 1.0, 1.0];
|
||||
|
||||
alpha = 0.01;
|
||||
l = 0.0;
|
||||
alpha = 1.0;
|
||||
l = 3;
|
||||
iterations = 1000;
|
||||
do_plot = false;
|
||||
|
||||
@@ -57,4 +57,4 @@ function hypothesis = get_hypothesis(x, theta)
|
||||
for t = 1:length(theta)
|
||||
hypothesis = hypothesis + theta(t) * (x ^ (t - 1));
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user