最近邻算法是机器学习最基本的算法,主要根据欧式距离进行判别,小白一枚~,不足地方请赐教!
题目:用最近邻算法(KNN)将待测点分类到三个已知类别中
代码:
X=[7,10,1,5,3,8,11,12,18,19,16,14,21,25,23,27,29,28,36,33,31,35,38,32,42,46,48,49,44,40,53,57,59,55,58,51,60,68,62,66,64,69,76,73,72,78,79,75,88,80,82,86,89,84];%Determine sample points
Y=[71,76,8,44,87,27,34,45,16,63,19,54,4,73,31,43,56,81,18,42,69,85,51 9,75,64,9,84,33 44,15,28,74,43,57,87,18,6,79,45,31,58,24,43,7,69,53,83,15,33,88,27,54,75];%Determine sample points
[r,c]=size(X);%to determine the size
[l,h]=size(Y);
%dmin=100;
%k=[0 5 10 15 20];
w1=[7,10,1,5,3,8,11,12,18,19,16,14,21,25,23,27,29,28];%Identify the sample points of the first class
y1=[71,76,8,44,87,27,34,45,16,63,19,54,4,73,31,43,56,81];
w2=[36,33,31,35,38,32,42,46,48,49,44,40,53,57,59,55,58,51];%Identify the sample points of the second class
y2=[18,42,69,85,51 9,75