一、解析
KNN: 接收数据集以及聚点信息 返回聚点轨迹,聚点簇,迭代次数,偏差以及偏差和
Dis_Func: 接收两点的坐标 返回两点距离 此处为欧式距离(可根据需求修改)
A_Rand: 接收数据集大小,分组数量,每组数量以及离散程度 返回随机生成有规律分布的数据集
Show: 接收聚点簇轨迹,并作出聚点移动轨迹
二、效果
1、数据展示
2、聚点迭代路径
‘+’表示数据集分布 ‘*’表示聚点起点 直线表示聚点迭代轨迹
三、代码
1、mian
%A:数据集
%size_A:数据集大小
%Points:聚点集
%nums_Points:聚点数量
%Error:误差和
%Max_Iteration:最大迭代次数
size_A=[20,2];
A=A_Rand(size_A,4,5,2);
%A=Temp;
plot(A(:,1),A(:,2),'+')
hold on;
nums_Points=4;
Points=50+5.*rand(nums_Points,size_A(2));
plot(Points(:,1),Points(:,2),'*')
Error=9;
Max_Iteration=20;
%Points:聚点轨迹
%Points_A:聚点簇
%flag:迭代次数
%Points_Centre_Variance:聚点簇偏差
%Points_Centre_Variance_Sum:聚点簇偏差和
[Track_Points,Points_A,flag, Points_Centre_Variance,Points_Centre_Variance_Sum]=KNN(A,size_A,Points,nums_Points,Error,Max_Iteration);
%聚点轨迹作图
Show(A,size_A,Track_Points, nums_Points,flag+1);
disp("最终聚点:");
disp(Track_Points((1+flag*nums_Points):(flag+1)*nums_Points,:));
disp("聚点簇:");
disp(Points_A);
disp("迭代次数:");
disp(flag);
disp("聚点簇偏差:");
disp(Points_Centre_Variance);
disp("聚点簇偏差和:");
disp(Points_Centre_Variance_Sum);
2、KNN
function [Track_Points,Points_A,flag, Points_Centre_Variance,Points_Centre_Variance_Sum] = KNN(A,size_A,Points,nums_Points,Error,Max_Iteration)
%A:���?�
%size_A:���?���С
%Points:�??
%nums_Points:�?���
%Error:���?�������
%聚点轨迹记录
Track_Points=[];
Track_Points=[Track_Points;Points];
%��������
flag=0;
%���?�?������ ����
Dist_A_P=zeros(size_A(1),1+nums_Points);
Dist_A_P(:,1)=(1:size_A(1))';
for i=1:size_A(1)
for j=1:nums_Points
Dist_A_P(i,j+1)=Dist_Func(A(i,:),Points(j,:),nums_Points);
end
end
%�?����� ���� ��
Points_A=zeros(nums_Points,2+size_A(1));
Points_A(:,1)=(1:nums_Points)';
Points_A(:,2)=0;
for i=1:size_A(1)
temp=find(Dist_A_P(i,2:1+nums_Points)==min(Dist_A_P(i,2:1+nums_Points)));
Points_A(temp(1),2)=Points_A(temp(1),2)+1;
Points_A(temp(1),2+Points_A(temp(1),2))=i;
end
%���?����� ���� �������?�����
Points_Centre=zeros(nums_Points,1+size_A(2));
Points_Centre(:,1)=(1:nums_Points)';
for i=1:nums_Points
Sum=zeros(1,size_A(2));
for j=1:Points_A(i,2)
Sum=Sum+A(Points_A(i,2+j),:);
end
if(Points_A(i,2)~=0)
Points_Centre(i,2:1+size_A(2))=Sum./Points_A(i,2);
end
end
%?������ ���� ?��
Points_Centre_Variance=zeros(nums_Points,2);
Points_Centre_Variance(:,1)=(1:nums_Points)';
Points_Centre_Variance_Sum=0;
for i=1:nums_Points
Sum=0;
for j=1:Points_A(i,2)
Sum=Sum+Dist_Func(Points(i,:),A(Points_A(i,2+j),:),nums_Points);
end
if(Points_A(i,2)~=0)
Points_Centre_Variance(i,2)=Sum./Points_A(i,2);
end
Points_Centre_Variance_Sum=Points_Centre_Variance_Sum+Points_Centre_Variance(i,2);
end
while(Points_Centre_Variance_Sum>Error && flag<Max_Iteration)
%����������1
flag=flag+1;
%����Points
for i=1:nums_Points
if(Points_A(i,2)~=0)
Points(i,:)=Points_Centre(i,2:1+size_A(2));
end
end
%聚点轨迹记录
Track_Points=[Track_Points;Points];
%���?�?������ ����
for i=1:size_A(1)
for j=1:nums_Points
Dist_A_P(i,j+1)=Dist_Func(A(i,:),Points(j,:),nums_Points);
end
end
%�?����� ���� ��
Points_A(:,2)=0;
for i=1:size_A(1)
temp=find(Dist_A_P(i,2:1+nums_Points)==min(Dist_A_P(i,2:1+nums_Points)));
Points_A(temp(1),2)=Points_A(temp(1),2)+1;
Points_A(temp(1),2+Points_A(temp(1),2))=i;
end
%���?����� ���� �������?�����
for i=1:nums_Points
Sum=zeros(1,size_A(2));
for j=1:Points_A(i,2)
Sum=Sum+A(Points_A(i,2+j),:);
end
if(Points_A(i,2)~=0)
Points_Centre(i,2:1+size_A(2))=Sum./Points_A(i,2);
end
end
%?������ ���� ?��
Points_Centre_Variance_Sum=0;
for i=1:nums_Points
Sum=0;
for j=1:Points_A(i,2)
Sum=Sum+Dist_Func(Points(i,:),A(Points_A(i,2+j),:),nums_Points);
end
if(Points_A(i,2)~=0)
Points_Centre_Variance(i,2)=Sum./Points_A(i,2);
end
Points_Centre_Variance_Sum=Points_Centre_Variance_Sum+Points_Centre_Variance(i,2);
end
end
end
3、Dist_Func
function [ret] = Dist_Func(A,B,nums_Points)
ret=round(sqrt(sum((A-B).^2)),3);
end
4、A_Rand
function [Ret_A] = A_Rand(size_A,nums1,nums2,Dispersion)
%size_A:数据集大小
%nums1:分组数量
%nums2:每组数量
%Dispersion:每组离散程度
temp=100.*rand(nums1,size_A(2));
Ret_A=Dispersion.*rand(size_A);
for i=1:nums1
Ret_A((nums2*(i-1)+1):(nums2*i),:)=Ret_A((nums2*(i-1)+1):(nums2*i),:)+temp(i,:);
end
end
5、Show
function [Ret_A] = Show(A,size_A,Track_Points,nums_Point,nums)
%A:数据集
%size_A:数据集大小
%Track_Points:聚点轨迹
%nums_Point:聚点数量
%nums:聚点规模
for i=1:nums_Point
temp1= Track_Points(nums_Point*[0:nums-1]+i,1);
temp2= Track_Points(nums_Point*[0:nums-1]+i,2);
plot(temp1,temp2);
end
|