Given the training data in the table below, Use Matlab to predict the class of t
ID: 3841520 • Letter: G
Question
Given the training data in the table below, Use Matlab to predict the class of the following new example using k-nearest neighbor for
k = 5: age <=30, income = high, student = yes, credit_rating=fair.
For similarity measure, use similarity (A,B) = where
are age, income, student or credit_rating
for income and 1 for student, age and credit_rating and
= 1 if and 0 otherwise.
age income student credit rating buys computer no no fair K-30 high 30 high no excellent no no fair 31...40 high yes yes >40 medium no fair yes >40 yes fair low no yes excellent >40 low 31...40 Dow yes excellent yes no 30 medium no fair K-30 low yes fair VeS >40 medium yes fair yes yes 30 medium yes excellent Wes 31...40 medium no excellent yes 31...40 high yes fair no >40 medium no excellentExplanation / Answer
%training set and categories accordinglt
trainingSet=[0 2 0 0 0
0 2 0 1 0
1 2 0 0 1
2 1 0 0 1
2 0 1 0 1
2 0 1 1 0
1 0 1 1 1
0 1 0 0 0
0 0 1 0 1
2 1 1 0 1
0 1 1 1 1
1 1 0 1 1
1 2 1 0 1
2 1 0 1 0];
ex=[0 2 1 0];
s=[]; _an=0;trainingSan=0;_SAN=0;
%Check and compute similarities
%for each row
for i=1:14
_SAN=0;
for j=1:4
if(trainingSet(i,j)==ex(j))
_an=1;
end
if(j==2)
trainingSan=_an*2;
else
trainingSan=_an;
end
_SAN+=trainingSan;
_an=0;
end
s(i)=_SAN;
end
%sorting similarities..
trainingSet(:,6)=s;
nts(:,1)=trainingSet(:,6);
nts(:,2)=trainingSet(:,5);
nts=sortrows(nts);
a=size(nts)(1);
nts=nts(a-4:end, :);
_ny=0; _nn=0;
%check 5 nearest neighbours
for i=1:5
if (nts(i,2)==0)
_nn+=1;
else
_ny+=1;
end
end
%group mojority as per example
if(_ny>_nn)
disp("** YES **");
else
disp("** NO **");
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.