-
-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathrunLinearRegression.m
59 lines (38 loc) · 1.58 KB
/
runLinearRegression.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
% This file runs univariate linear regression to predict profits of food trucks based on previous
% actual values of profits in $10,000s in various cities with populations in 10,000s respectively.
clear ; close all; clc ;
fprintf('Plotting data\n');
data = load('data_.txt');
x = data(:, 1); Y = data(:, 2);
n = length(Y); % Number of training examples.
plotdata(x, Y);
fprintf('Program paused, press enter to continue\n');
pause;
x = [ones(n, 1), data(:,1)];
Theta = zeros(2, 1);
noi = 1500; % Number of iterations in gradient descent.
Alpha = 0.01; % Learning rate.
fprintf('Testing the cost function\n')
j = computecost(x, Y, Theta);
fprintf('With Theta = [0 ; 0]\nCost computed = %f\n', j);
fprintf('Expected cost value (approx) 32.07\n');
j = computecost(x, Y, [-1 ; 2]);
fprintf('With theta = [-1 ; 2]\nCost computed = %f\n', j);
fprintf('Expected cost value (approx) 54.24\n');
fprintf('Program paused, press enter to continue\n');
pause;
fprintf('Running gradient descent\n');
Theta = gradientdescent(x, Y, Theta, Alpha, noi);
fprintf('Theta found by gradient descent\n');
fprintf('%f\n', Theta);
fprintf('Expected Theta vector (approx)\n');
fprintf(' -3.6303\n 1.1664\n\n');
hold on; % To plot hypothesis on data.
plot(x(:, 2), x * Theta, '-');
legend('Training data', 'Linear regression');
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);