clc
clear all
close all
rate=0.02;
data = load('data.txt');
datay=data(:, 3);
datay=datay(1:10, :);
data = data(:, 1:2);
data = data(1:12, :);
label = [ones(5, 1); zeros(5, 1)];
plot(data(:,1),data(:,2),'*');
trainData = data(1:10, :); % 每组前10个用于训练
testData = data(11:12, :); % 最后2组个用于测试
testData =[ones(size(testData,1),1) testData]
trainData =[ones(size(trainData,1),1) trainData]
q=[1;1;1];
h=trainData*q
g=(1./(1+exp(-h)));
for i=1:100000
q=q-(rate/10)*((g-datay)'*trainData)'
h=trainData*q;
g=(1./(1+exp(-h)));
df=((g-datay)'*trainData)';
if abs(min(df))<0.01
break;
end
end
h=testData*q;
testReult=(1./(1+exp(-h)));
原始数据,最后一列的数据没有用到。
5.1 3.5 1 0.2
0.5 3.0 0 0.2
1 3.2 0 0.2
1.1 3.1 0 0.2
3 3.6 1 0.2
6 3.9 1 0.4
6 1 1 0.3
3 5 1 0.2
2 2.9 0 0.2
3 3.8 1 0.1
1 1.8 0 0.1
3 6.8 1 0.1