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)