You are on page 1of 2

function [ conf,Acc ] = Perceptron_image( training_data,test_data )

n1=length(training_data);
n2=length(test_data);
if(n1~=n2)
error('input data is not good enough');
end
X1=[training_data{1}',training_data{2}'];
X2=[training_data{2}',training_data{3}'];
X3=[training_data{1}',training_data{3}'];
n1=size(training_data{1},1);
n2=size(training_data{2},1);
n3=size(training_data{3},1);
Y1=[ones(1,n1),-ones(1,n2)];
Y2=[ones(1,n2),-ones(1,n3)];
Y3=[ones(1,n3),-ones(1,n1)];
w=[.5 .5]';
wtag1=perceptron(X1,Y1,w);
wtag2=perceptron(X2,Y2,w);
wtag3=perceptron(X3,Y3,w);
test1=test_data{1};
test2=test_data{2};
test3=test_data{3};
m1=size(test1,1);
m2=size(test2,1);
m3=size(test3,1);
for i=1:m1
testx1(1,i)=wtag1'*test1(i,:)';
testx1(2,i)=wtag2'*test1(i,:)';
testx1(3,i)=wtag3'*test1(i,:)';
end
for i=1:m2
testx2(1,i)=wtag1'*test2(i,:)';
testx2(2,i)=wtag2'*test2(i,:)';
testx2(3,i)=wtag3'*test1(i,:)';
end
for i=1:m2
testx3(1,i)=wtag1'*test3(i,:)';
testx3(2,i)=wtag2'*test3(i,:)';
testx3(3,i)=wtag3'*test3(i,:)';
end
conf(1,1)=length(find(testx1(1,:)>=0 & testx1(3,:)>=0))/m1;
conf(1,2)=length(find(testx1(1,:)<0 & testx1(2,:)>=0))/m1;
conf(1,3)=length(find(testx1(2,:)<0 & testx1(3,:)<0))/m1;
conf(2,1)=length(find(testx2(1,:)>=0 & testx2(3,:)>=0))/m2;
conf(2,2)=length(find(testx2(1,:)<0 & testx2(2,:)>=0))/m2;
conf(2,3)=length(find(testx2(2,:)<0 & testx2(3,:)<0))/m2;

conf(3,1)=length(find(testx3(1,:)>=0 & testx3(3,:)>=0))/m3;


conf(3,2)=length(find(testx3(1,:)<0 & testx3(2,:)>=0))/m3;
conf(3,3)=length(find(testx3(2,:)<0 & testx3(3,:)<0))/m3;
confusion_matrix(conf,3);
Acc=(length(find(testx1(1,:)>=0 & testx1(3,:)>=0))+length(find(testx2(1,:)<0 & t
estx2(2,:)>=0))+length(find(testx3(2,:)<0 & testx3(3,:)<0)))/sum(m1+m2+m3);
end

You might also like