Skip to content

Instantly share code, notes, and snippets.

@TylerL-uxai
Created October 3, 2015 15:15
Show Gist options
  • Save TylerL-uxai/6fd68c09ce29663020cd to your computer and use it in GitHub Desktop.
Save TylerL-uxai/6fd68c09ce29663020cd to your computer and use it in GitHub Desktop.
Running Gradient Descent ...
ans =
32.0727
Theta found by gradient descent: 0.000000 0.000000
For population = 35,000, we predict a profit of 0.000000
For population = 70,000, we predict a profit of 0.000000
============================
%% =================== Part 3: Gradient descent ===================
fprintf('Running Gradient Descent ...\n')
X = [ones(m, 1), data(:,1)]; % Add a column of ones to x
theta = zeros(2, 1); % initialize fitting parameters
% Some gradient descent settings
iterations = 1500;
alpha = 0.01;
% compute and display initial cost
computeCost(X, y, theta)
% run gradient descent
theta = gradientDescent(X, y, theta, alpha, iterations);
% print theta to screen
fprintf('Theta found by gradient descent: ');
fprintf('%f %f \n', theta(1), theta(2));
% Plot the linear fit
hold on; % keep previous plot visible
plot(X(:,2), X*theta, '-')
legend('Training data', 'Linear regression')
hold off % don't overlay any more plots on this figure
% Predict values for population sizes of 35,000 and 70,000
predict1 = [1, 3.5] *theta;
fprintf('For population = 35,000, we predict a profit of %f\n',...
predict1*10000);
predict2 = [1, 7] * theta;
fprintf('For population = 70,000, we predict a profit of %f\n',...
predict2*10000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment