线性回归问题(小鸡版)

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的时候的拟合曲线

"Mstar Bin Tool"是一款专门针对Mstar系列芯片开发的固件处理软件,主要用于智能电视及相关电子设备的系统维护与深度定制。该工具包特别标注了"LETV USB SCRIPT"模块,表明其对乐视品牌设备具有兼容性,能够通过USB通信协议执行固件读写操作。作为一款专业的固件编辑器,它允许技术人员对Mstar芯片的底层二进制文件进行解析、修改与重构,从而实现系统功能的调整、性能优化或故障修复。 工具包中的核心组件包括固件编译环境、设备通信脚本、操作界面及技术文档等。其中"letv_usb_script"是一套针对乐视设备的自动化操作程序,可指导用户完成固件烧录全过程。而"mstar_bin"模块则专门处理芯片的二进制数据文件,支持固件本的升级、降级或个性化定制。工具采用7-Zip压缩格式封装,用户需先使用解压软件提取文件内容。 操作前需确认目标设备采用Mstar芯片架构并具备完好的USB接口。建议预先备份设备原始固件作为恢复保障。通过编辑器修改固件参数时,可调整系统配置、增删功能模块或修复已知缺陷。执行刷机操作时需严格遵循脚本指示的步骤顺序,保持设备供电稳定,避免中断导致硬件损坏。该工具适用于具备嵌入式系统知识的开发人员或高级用户,在进行设备定制化开发、系统调试或维护修复时使用。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值