Practice Problem on k-NN Classification | MATLAB
%Code:
clc
clear all
close all
warning off
output=[];
x=input('Enter
the first parameter');
y=input('Enter
the second parameter:');
c=input('Enter
the class');
m=c;
k=input('Enter
the k value');
es=input('For
how many times you want to run the code:');
for
erts=1:es
a=input('Enter
the first parameter value of the test:');
b=input('Enter
the second parameter value of the test:');
distance=[];
for
i=1:length(x)
e=sqrt((x(i)-a)^2+(y(i)-b)^2);
distance=[distance e];
end
distance
temp=0;
gemp=0;
for
i=1:length(distance)
for j=1:(length(distance)-i)
if(distance(j)>distance(j+1))
temp=distance(j);
distance(j)=distance(j+1);
distance(j+1)=temp;
gemp=c(j);
c(j)=c(j+1);
c(j+1)=gemp;
end
end
end
classy=[];
for
i=1:k
classy=[classy c(i)];
end
output=[output mode(classy)];
c=m;
end
No comments