码迷,mamicode.com
首页 > 其他好文 > 详细

ML基础教程:线性建模的非线性响应

时间:2016-07-23 09:09:05      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:

synthquad.m


Generate a synthetic dataset from a quadratic function

N = 200; %Number of data points
% Generate random x values between -5 and 5
x = 10*(sort(rand(N,1))-0.5);

Define the function and the true parameters

t=w0+w1x+w2x2

w_0 = 1;
w_1 = -2;
w_2 = 0.5;

Define t and add some noise

t = w_0 + w_1*x + w_2*(x.^2);
t = t + 0.5*randn(N,1);

Plot the data

figure(1);
hold off
plot(x,t,‘k.‘,‘markersize‘,10);
xlabel(‘x‘);
ylabel(‘t‘);

技术分享

Fit the quadratic model and a linear model for comparison
Using vector matrix notation

X = [];
for k = 0:2
    X = [X x.^k];
    if k == 1
        linear_w = inv(X‘*X)*X‘*t;
    end
    if k == 2
        quad_w = inv(X‘*X)*X‘*t;
    end
end

fprintf(‘\n Linear function: t = %g + %g x‘,linear_w(1),linear_w(2));
fprintf(‘\n Quadratic function: t = %g + %g x + %g x^2‘,...
    quad_w(1),quad_w(2),quad_w(3));

output

Linear function: t = 5.26187 + -1.98859 x
Quadratic function: t = 0.949436 + -2.00242 x + 0.50068 x^2

Plot the functions

plotx = [-5:0.01:5]‘;
plotX = [];
for k = 0:2
    plotX = [plotX plotx.^k];
end

figure(1);hold off;
% Add the data again
plot(x,t,‘k.‘,‘markersize‘,10);
xlabel(‘x‘);
ylabel(‘t‘);
hold on
plot(plotx,plotX*quad_w,‘r‘,‘linewidth‘,2);
plot(plotx,plotX(:,1:2)*linear_w,‘g‘,‘linewidth‘,2);

legend(‘Data‘,‘Quadratic‘,‘Linear‘)

技术分享

olymppoly.m


Load the Olympic data and extract the mens 100m data

load data/olympics.mat

x = male100(:,1);
t = male100(:,2);

% Rescale x for numerical reasons
x = x - x(1);
x = x./4;

% Plot the data
figure(1);hold off
plot(x,t,‘bo‘,‘markersize‘,10);
xlabel(‘Olympic number (note, not year!)‘);
ylabel(‘Winning time‘);

技术分享
Linear model

plotx = [x(1)-2:0.01:x(end)+2]‘;
X = [];
plotX = [];
for k = 0:1
    X = [X x.^k];
    plotX = [plotX plotx.^k];
end

w = inv(X‘*X)*X‘*t;

% Plot the model
figure(1);hold off
figure(1);hold off
plot(x,t,‘bo‘,‘markersize‘,10);
xlabel(‘Olympic number (note, not year!)‘);
ylabel(‘Winning time‘);
hold on
plot(plotx,plotX*w,‘r‘,‘linewidth‘,2)

技术分享
Quadratic model

plotx = [x(1)-2:0.01:x(end)+2]‘;
X = [];
plotX = [];
for k = 0:2
    X = [X x.^k];
    plotX = [plotX plotx.^k];
end

w = inv(X‘*X)*X‘*t;

% Plot the model
figure(1);hold off
figure(1);hold off
plot(x,t,‘bo‘,‘markersize‘,10);
xlabel(‘Olympic number (note, not year!)‘);
ylabel(‘Winning time‘);
hold on
plot(plotx,plotX*w,‘r‘,‘linewidth‘,2)

技术分享
Quartic model

plotx = [x(1)-2:0.01:x(end)+2]‘;
X = [];
plotX = [];
for k = 0:4
    X = [X x.^k];
    plotX = [plotX plotx.^k];
end

w = inv(X‘*X)*X‘*t;

% Plot the model
figure(1);hold off
figure(1);hold off
plot(x,t,‘bo‘,‘markersize‘,10);
xlabel(‘Olympic number (note, not year!)‘);
ylabel(‘Winning time‘);
hold on
plot(plotx,plotX*w,‘r‘,‘linewidth‘,2)

技术分享

关于Machine Learning更多讨论与交流,敬请关注本博客和新浪微博songzi_tea.

ML基础教程:线性建模的非线性响应

标签:

原文地址:http://blog.csdn.net/songzitea/article/details/51996911

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!