k nearest neighbour classifier Matlab
Pattern Recognition
K-Nearest Neighbor Classification
K nearest neighbors is a simple algorithm that stores all available cases and classifies new cases based on a similarity measure (e.g., distance functions).
Example
CODES
clear;
hold on;
axis equal;
axis([0 1 0 1]);
for i=1:10;
x(i,:)=ginput(1);
text(x(i,1), x(i,2), '1');
plot(x(i,1), x(i,2), '.');
end;
for i=11:20;
x(i,:)=ginput(1);
text(x(i,1), x(i,2), '2');
plot(x(i,1), x(i,2), '.');
end;
for j=1:4;
display('please input new sample with mouse');
x1(j,:)=ginput(1)
for i=1:20; dx(i)=norm(x1(j,:)- x(i,:));
end;
[xd,id] = sort(dx);
kd=floor(id(1:3)/10-1e-3)+1 ;
counter1=0;
counter2=0
for n=1:3;
if kd(n)==1
counter1=counter1+1;
else
counter2=counter2 +1;
end
end;
if counter1>=2
class=1
else
class=2
end
switch class
case 1
M = 1
case 2
M = 2
otherwise
error('This is impossible')
end
switch j
case 1
s = 'A';
case 2
s = 'B';
case 3
s = 'C';
case 4
s = 'D';
otherwise
error('This is impossible')
end
text(x1(j,1), x1(j,2), s)
plot([x1(j,1) x(id(1),1)],[x1(j,2) x(id(1),2)],'-');
plot([x1(j,1) x(id(2),1)],[x1(j,2) x(id(2),2)],'-');
plot([x1(j,1) x(id(3),1)],[x1(j,2) x(id(3),2)],'-');
end;
Yorumlar
Yorum Gönder