斯坦福《机器学习》课程算法matlab实现之梯度下降算法——直线回归(一)

梯度下降算,可用于回归,也可用于分类,下面是该算法的最简单的演示,学习率alpha对算法的收敛影响很大,取大了不收敛,过小过大迭代次数增加;


clear;clc;
data=genlineardata(13,-3.36,100);
%生成在直线y=13-3.36*x周围的100个点存入data[100*3],data第一列全为1

theta=zeros(2,1);
theta(1)=0;
theta(2)=1;
times=0;
alpha=0.1;
h=0;
distant=0;


sum0=0;
sum1=0;






[row,col]=size(data);
figure;
plot(data(:,2),data(:,3),'r.');
x=-5:5;




switch(3)
    case 1,
 % stochastic gradient descent.
 
    while(1)
        y=theta(2)*x+theta(1);
        hold on;
        plot(x,y,'r');
        temp0=theta(1);
        temp1=theta(2);


        for i=1:row
            h=theta(1)+theta(2)*data(i,2);
            alpha=0.2/i;
            sum0=(data(i,3)-h);
            sum1=(data(i,3)-h)*data(i,2);
            theta(1)=theta(1)+alpha*sum0;
            theta(2)=theta(2)+alpha*sum1; 
        end
        distant=abs(theta(1)-temp0)+abs(theta(2)-temp1);
        if(distant<0.001)
            break;
        end
        times=times+1;
        if(times>20)
            break;
        end
    end


    
case 2,
% batch gradient descent.


    while(1)
        
        y=theta(2)*x+theta(1);
        hold on;
        plot(x,y,'r');
        temp0=theta(1);
        temp1=theta(2);


        sum0=0;
         sum1=0;
         times=times+1;
         alpha=1/(times*row);
        for i=1:row
            h=theta(1)+theta(2)*data(i,2);
            sum0=sum0+alpha*(data(i,3)-h);
            sum1=sum1+alpha*(data(i,3)-h)*data(i,2);  
        end
        theta(1)=theta(1)+sum0;
        theta(2)=theta(2)+sum1/2; 
        distant=abs(theta(1)-temp0)+abs(theta(2)-temp1);
        if(distant<0.001)
            break;
        end
        if(times>20)
            break;
        end
    end
case 3,
%Least squares revisited
y=theta(2)*x+theta(1);
hold on;
plot(x,y,'r');
        
X=data(:,1:2);
Y=data(:,3);
theta=(X'*X)\X'*Y;
   
end
theta(1)
theta(2)
times
y=theta(2)*x+theta(1);
hold on;
plot(x,y,'g');
% times



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值