Logic Regression----MATLAB实现

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

function logic_regression()
x = rand(300,2);%%5003列
y = zeros(size(x,1),1);
[row,col] = size(x);
for i = 1:row
    sum = 0;
    for j = 1:col
        sum = sum + x(i,j);
    end
    if(sum >= 1)
            y(i,1) = 1;
     else
            y(i,1) = 0;
     end
end
%加一个常数项
x = [x,ones(size(x,1),1)];
col = col + 1;
randIndex = randperm(size(x,1));
new_x = x(randIndex,:);
new_y = y(randIndex,:);
% 70%数据训练,30%数据测试
number = row * 0.7;
train_x = new_x(1:number,:);
test_x = new_x(number+1:row,:);
train_y = new_y(1:number,:);
test_y = new_y(number+1:row,:);
[train_row,train_col] = size(train_x);
[test_row,test_col] = size(test_x);
%%%%%%%%%%%%%%%%%%%%%%%%
w = rand(1,size(x,2));%w
a = 0.01;%学习因子

%训练
poche = 500;
l = [];
while(poche)
    sum = zeros(1,size(x,2));
    loss = 0;
    for i = 1:train_row
        f = 1.0/(1 + exp(-1.0*(w*x(i,:)')));%概率p
        sum = sum + (y(i,1) - f)*x(i,:);%dl/dw
        loss = loss + y(i,1)*log(f) + (1-y(i,1))*log(1.0-f);%记录损失值
    end
    w2 = w + a*sum;
    w = w2;
    poche = poche - 1;
    loss = -loss/train_row;
    l = [l,loss];
    if loss < 0.01
        break;
    end
end
figure
subplot(1,2,1)
plot(l)
title('loss')
 
subplot(1,2,2)
show_x=0:0.1:10;
show_y=(-w(1)*show_x-w(3))/w(2);
plot(show_x,show_y,'linewidth',2)
hold on
plot(x(y==1,1),x(y==1,2),'ro')
hold on
plot(x(y==0,1),x(y==0,2),'go')
axis([0 1 0 1])

accarancy = 0;
for i = 1:test_row
    f = 1.0/(1.0 + exp(-w*test_x(i,:)'));
    if f >= 0.5 && test_y(i,1) == 1
        accarancy = accarancy + 1;
    end
    if f < 0.5 && test_y(i,1) == 0
        accarancy = accarancy + 1;
    end
end
accarancy/test_row


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值