二元分类,逻辑回归,标准梯度下降,octave实现

clear
clc
 
%% 数据准备
%X = xlsread('C:\Users\user01\Desktop\test.xlsx');
%二分类 随机生成数据。  200个数据  每个数据2个特征
data=1*rand(3000,2);
label=zeros(3000,1);
%label(sqrt(data(:,1).^2+data(:,2).^2)<8)=1;
label((data(:,2)+data(:,1)>1))=1;
%在data上加常数特征项;
data=[data,ones(size(data,1),1)];
 
%打乱循序
randIndex = randperm(size(data,1));
data_new=data(randIndex,:);
label_new=label(randIndex,:);

x = data_new
y = label_new

thetal = [2 4 3]

h =1./(1+exp(-(x * thetal')));
cost = y .* log(h) + (1 - y) .* log(1 - h)
j = - sum(cost) / size(x,1)

while j > 0.06
  thetal =  thetal - 5 * sum((h - y).*x)/size(x,1)
 
  h =1./(1+exp(-(x * thetal')));
  cost = y .* log(h) + (1 - y) .* log(1 - h)
  j = - sum(cost) / size(x,1)
 
end

%测试数据,成功率达到0.99
testdata=1*rand(300,2);
testdata=[testdata,ones(size(testdata,1),1)];
testlable=zeros(300,1);
testlable((testdata(:,2)+testdata(:,1)>1))=1;

testh =1./(1+exp(-(testdata * [ 10.28    10.185    -10.189]')));
testh(testh > 0.5)=1
testh(testh < 0.5)=0
testres = (testh == testlable )
redio = sum(testres)/300
disp(redio)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值