逻辑(logistic)回归

本文详细介绍了逻辑回归算法的基本原理,包括Sigmoid函数的应用、损失函数的定义及其计算方式,并通过一个简单的实验进行了验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

回忆

----------------------------------------------------------------------------------------------------------------------

训练集合



其中有n个特征,只有一个,那么假如有如下关系:


则:

-----------------------------------------------------------------------------------------------------------------------

Logistic函数(Sigmoid函数):



则:




cost function:


                     

其中:


计算,




处理步骤:


其中:


现在来证明logistic函数和线性回归函数的梯度表达是一样的:

线性回归的梯度:

logistic的梯度:



又因为:

那么:



其中z就是式中的h,所以和线性回归一样的梯度函数:

                                                     


实验:

%逻辑回归

%初始数据  
x=[-3;      -2;     -1;     0;      1;      2;     3];  
y=[0.01;    0.1;   0.3;    0.45;   0.8;    0.8;    0.99];  
plot(x,y,'ro');  
hold on  

%拟合
m = length(y);
theta = [0 0];  
a=0.005;  
loss = 1;  
iters = 1;  
eps = 0.0001;  
while loss >eps && iters <100
    loss = 0;
    for i = 1:length(y)
        h = 1./(1+exp(-(theta(1)+theta(2)*x(i))));
        theta(1)=theta(1)+a*(y(i)-h);  
        theta(2)=theta(2)+a*(y(i)-h)*x(i,1);  
        err = theta(1)+theta(2)*x(i,1)-y(i);  
        loss = loss+err*err/m; 
    end
     iters = iters+1;
end
iters  
theta 

%画图对比
for x = -3:0.01:3
     h = 1./(1+exp(-(theta(1)+theta(2)*x)));
     plot(x,h);  
end
hold off






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值