logistic regression二元分类(小鸡版)

本文介绍了如何使用Matlab实现Logistic Regression进行二分类任务,包括数据准备、初始化参数、梯度下降法更新参数等步骤,并展示了不同数据分布下的分类效果。

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

1.logistic regression回归解决二分类

matlab 代码

% logistic regression 二元分类,使用 batch descent regression算法

%数据
x = linspace(1,20,20)
y = fliplr([0 0 1 0 0 0 1 0 1 1 1 1 1 1  1 1 1 1 1 1]); 

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

step = 0.001; %步长
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) - 1 ./ (1 + exp( - (theta0+theta1*x(i))  ) )) *1
       derative_theta1 = derative_theta1 + (y(i) - 1 ./ (1 + exp( - (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.00000003
        f = 1./( 1+ exp(-(theta0 + theta1*x)))
        func = sprintf('%d times: theta1=%f theta0=%f\n',count,theta1,theta0);
        plot(x,y,'r+',x,f,'linewidth',2); 
        legend(func)
        break;
   end 
   count = count + 1
end 

2.分类情况
y =fliplr([0 0 1 0 0 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1]) 时候的的分类情况

y = [0 0 1 0 0 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1]时候的的分类情况


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值