[regression] updated exercise infrastructure
This commit is contained in:
@@ -1,25 +0,0 @@
|
||||
% data:
|
||||
load('lin_regression.mat')
|
||||
|
||||
% compute mean squared error for a range of slopes and intercepts:
|
||||
slopes = -5:0.25:5;
|
||||
intercepts = -30:1:30;
|
||||
errors = zeros(length(slopes), length(intercepts));
|
||||
for i = 1:length(slopes)
|
||||
for j = 1:length(intercepts)
|
||||
errors(i,j) = lsqError([slopes(i), intercepts(j)], x, y);
|
||||
end
|
||||
end
|
||||
|
||||
% minimum of error surface:
|
||||
[me, mi] = min(errors(:));
|
||||
[ia, ib] = ind2sub(size(errors), mi);
|
||||
eparams = [errors(ia), errors(ib)];
|
||||
|
||||
% gradient descent:
|
||||
pstart = [-2. 10.];
|
||||
[params, errors] = descent(x, y, pstart);
|
||||
|
||||
% comparison:
|
||||
fprintf('descent: %6.3f %6.3f\n', params(1), params(2));
|
||||
fprintf('errors: %6.3f %6.3f\n', eparams(1), eparams(2));
|
||||
@@ -1,15 +0,0 @@
|
||||
function [params, errors] = descent(xdata, ydata, pstart)
|
||||
mingradient = 0.1;
|
||||
eps = 0.01;
|
||||
|
||||
errors = [];
|
||||
params = pstart;
|
||||
count = 1;
|
||||
gradient = [100.0, 100.0];
|
||||
while norm(gradient) > mingradient
|
||||
gradient = lsqGradient(params, xdata, ydata);
|
||||
errors(count) = lsqError(params, xdata, ydata);
|
||||
params = params - eps .* gradient;
|
||||
count = count + 1;
|
||||
end
|
||||
end
|
||||
@@ -1,22 +0,0 @@
|
||||
clear
|
||||
close all
|
||||
load('lin_regression.mat')
|
||||
|
||||
pstart = [-2. 10.];
|
||||
[params, errors] = descent(x, y, pstart);
|
||||
|
||||
figure()
|
||||
subplot(2,1,1)
|
||||
hold on
|
||||
scatter(x, y, 'displayname', 'data')
|
||||
xx = min(x):0.01:max(x);
|
||||
fx = params(1)*xx + params(2);
|
||||
plot(xx, fx, 'displayname', 'fit')
|
||||
xlabel('Input')
|
||||
ylabel('Output')
|
||||
grid on
|
||||
legend show
|
||||
subplot(2,1,2)
|
||||
plot(errors)
|
||||
xlabel('optimization steps')
|
||||
ylabel('error')
|
||||
@@ -1,18 +0,0 @@
|
||||
% data:
|
||||
load('lin_regression.mat')
|
||||
|
||||
% gradient descent:
|
||||
pstart = [-2. 10.];
|
||||
[params, errors] = descent(x, y, pstart);
|
||||
|
||||
% lsqcurvefit:
|
||||
line = @(p, x) x.* p(1) + p(2);
|
||||
cparams = lsqcurvefit(line, pstart, x, y);
|
||||
|
||||
% polyfit:
|
||||
pparams = polyfit(x, y, 1);
|
||||
|
||||
% comparison:
|
||||
fprintf('descent: %6.3f %6.3f\n', params(1), params(2));
|
||||
fprintf('lsqcurvefit: %6.3f %6.3f\n', cparams(1), cparams(2));
|
||||
fprintf('polyfit: %6.3f %6.3f\n', pparams(1), pparams(2));
|
||||
Reference in New Issue
Block a user