Info
此问题已关闭。 请重新打开它进行编辑或回答。
how can ı determine classes in k-nn
2 次查看(过去 30 天)
显示 更早的评论
My problem is as follows: I have one unknown value and two classes. I calculate the distances from an unknown value to every known value, and I make one matrix. But now I have to take the three smallest distances and I must determine their class, and I don't know how I can take these three values and determine their class and show it to the user
here is my code;
clc
clear;
clear all;
A=[1 2; 2 2; 1.5 3; 3 4];
B=[4 5; 7 5; 7 6; 9 7];
k=3;
x1=input('pls enter x ');
y1=input('pls enter y ');
for i=1:1:4
xa=A(i,1);
ya=A(i,2);
da=sqrt((x1-xa)^2+(y1-ya)^2);
for h=1:length(da)
danew(i)=da(h);
end
end
danew
for j=1:1:4
xb=B(j,1);
yb=B(j,2);
db=sqrt((x1-xb)^2+(y1-yb)^2);
for m=1:length(db)
dbnew(j)=db(m);
end
end
dbnew
dd=[danew dbnew]
for p=1:length(dd)
for r=length(dd):-1:1+p
if dd(r)<dd(r-1);
gecici=dd(r-1);
dd(r-1)=dd(r);
dd(r)=gecici;
end
end
end
dd
0 个评论
回答(1 个)
Walter Roberson
2018-6-16
If you use https://www.mathworks.com/help/stats/knnsearch.html#namevaluepairarguments with the 'K', 3 option to look for the three shortest distances, then what you get back is indices of points. You would use the indices of the points to access your list of classes.
(I suspect you are trying to do majority voting.)
1 个评论
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!