线性回归问题(小鸡版)

1.使用 batch gradient descent 解决线性回归问题(小鸡版)

matlab还不熟代码有些冗余,以后优化到战斗鸡版

matlab代码
%  使用 batcch gradient descent 解决线性回归  

%data 
x = [1:50];
y = [4554 3014 2171 1891 1593 1532 1416 1326 1297 1266 ...
	1248 1052 951 936 918 797 743 665 662 652 ...
	629 609 596 590 582 547 486 471 462 435 ...
	424 403 400 386 386 384 384 383 370 365 ...
	360 358 354 347 320 319 318 311 307 290 ];

%初始值
theta0 = 0;
theta1 = 0;

step = 0.00001; %步长
count = 1 %记录迭代次数

while 1
   %(derative_theta0 ,derative_theta1)是梯度 
   derative_theta0 = 0;
   derative_theta1 = 0;
   
   m = length(x);
   for i=1 : length(x)
       derative_theta0 = derative_theta0 + (y(i) - (theta0+theta1*x(i)) )*1 ;
       derative_theta1 = derative_theta1 + (y(i) - (theta0+theta1*x(i)) )*x(i);
   end 

   %梯度下降或去新的 theta0和theta1
   theta0 = theta0 + step*derative_theta0;
   theta1 = theta1 + step*derative_theta1;
   
   %退出条件
   error = (step*derative_theta0)^2 + (step*derative_theta1)^2;
   if  error <  0.000001
        f = theta0 + theta1*x
        func = sprintf('%d times: f = %fx+%f\n',count,theta1,theta0);
        plot(x,y,'r+',x,f,'linewidth',1); 
        legend(func)
        break;
   end 
   count = count + 1
end 

拟合结果


2.Normal Equation 用矩阵运算直接得到参数theta

matlab代码
%线性回归 使用矩阵计算theta 
%如果数据量小 可以使用这种方法
x = [1:50];
y = [4554 3014 2171 1891 1593 1532 1416 1326 1297 1266 ...
	1248 1052 951 936 918 797 743 665 662 652 ...
	629 609 596 590 582 547 486 471 462 435 ...
	424 403 400 386 386 384 384 383 370 365 ...
	360 358 354 347 320 319 318 311 307 290 ];

x = x'
y = y'
x=[ones(length(x),1),x]
thetas = ((x'*x)^(-1))*x'*y %使用推算出来的矩阵运算公式得到 theta1 theta0
theta0 = thetas(1)
theta1 = thetas(2)

f = theta0 + theta1*x %得到的拟合函数
func = sprintf('f = %fx+%f\n',theta1,theta0);
x_ = x'
plot(x_(2,:),y,'r+',x,f,'linewidth',1);
legend(func)
  
拟合情况

3.Locally weighted linear regression 使用 batch gradient descent 算法

matlab代码
% 局部加权线性回归 local weighted linear regression 
% 是一个非参学习算法 no-parametric 算法,不像随机递归下降,它的结果是学习到theta保存以后是使用就可以了
% 非参学习算法 每一个预测都需要重新计算

%data 
x = [1:50];
y = [4554 3014 2171 1891 1593 1532 1416 1326 1297 1266 ...
	1248 1052 951 936 918 797 743 665 662 652 ...
	629 609 596 590 582 547 486 471 462 435 ...
	424 403 400 386 386 384 384 383 370 365 ...
	360 358 354 347 320 319 318 311 307 290 ];

xpoint = 6.5 %在取这个点的时候的拟合曲线

tao = 10;%bandwidth
wis = [] %存放权重,离xpoint越近权重越高
for i = 1: length(x)
    wi = exp( -((xpoint - x(i))/tao)^2/2);
    wis = [wis,wi]
end 

%初始值
theta1 = 0
theta0 = 0

step = 0.00001; %步长
count = 1 %迭代次数

while 1
   %(derative_theta0 ,derative_theta1)是梯度 
   derative_theta0 = 0;
   derative_theta1 = 0;
   
   m = length(x);
   for i=1 : length(x)
       derative_theta0 = derative_theta0 + (y(i) - (theta0+theta1*x(i)) )*1*wis(i);%乘以权重 离xpoint越远的地方影响越小
       derative_theta1 = derative_theta1 + (y(i) - (theta0+theta1*x(i)) )*x(i)*wis(i);
   end 

   %梯度下降取新的 theta0和theta1
   theta0 = theta0 + step*derative_theta0;
   theta1 = theta1 + step*derative_theta1;
   
   error = (step*derative_theta0)^2 + (step*derative_theta1)^2;
   
   %退出条件
   if  error <  0.000001 
        f = theta0 + theta1*x
        func = sprintf('在x=%f的拟合情况,迭代%d次 : f = %fx+%f\n',xpoint,count,theta1,theta0);
        plot(x,y,'r+',x,f,'linewidth',1); 
        legend(func)
        break;
   end 
   count = count + 1
end 
拟合情况
在x为6.5时候的拟合情况

在x为30.5的时候的拟合曲线

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值