Impact-Site-Verification: dbe48ff9-4514-40fe-8cc0-70131430799e

Search This Blog

Building a k-Nearest Neighbor algorithm with the Iris dataset | MATLAB


%Code:

clc
clear all
close all
warning off
load fisheriris
x=meas(:,3);
y=meas(:,4);
c=species;
output=[];
m=c;
k=input('Enter the k value');
ersa=input('Enter number of test datapoints:');
for i=1:ersa
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
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{i}=c{i};
end
tabulate(classy);
classy=string(classy);
output=[output mode(classy)];
c=m;
end


No comments

Popular Posts

Followers