clear;clc
load simulate2020625;
k=max(subcluster(:,3));%设置聚类数目
data = subcluster(:,1:2);
cata = subcluster;
[m,n]=size(data);
c1=subcluster(:,3);
c1=c1';
%% 实际聚类图
figure(1);
plot(cata(cata(:,3)==1,1),cata(cata(:,3)==1,2),'r*')
hold on
plot(cata(cata(:,3)==2,1),cata(cata(:,3)==2,2),'b*')
hold on
plot(cata(cata(:,3)==3,1),cata(cata(:,3)==3,2),'g*')
hold on
plot(cata(cata(:,3)==4,1),cata(cata(:,3)==4,2),'y*')
title('实际聚类图')
grid on;
%%
pattern=zeros(m,n+1);
center=zeros(k,n);%初始化聚类中心
pattern(:,1:n)=data(:,:);
%% 找到空间中距离最远两点
U = zeros(k,n);
temp = pdist(data);
temp = squareform(temp); %%转化成两两距离矩阵。
[~,t1] = max(temp);
[~,t2] = max(max(temp));
row = t1(t2);
col = t2;
%行,列分别对应一个节点。
U(1,:) = data(row,:);
U(2,:) = data(col,:);
%% 选择U集合,得初始聚类中心
record = data;
record([row col],:) = [];
N = 2;
if k>2
while N~=k
x = 0;
y = 0;
rlength = size(record,1);
ulength = size(U,1);
point_to_centre_distance = 0;
temp_u = zeros(ulength,1);
for i = 1:rlength
for j = 1:ulength
temp_u(j) = norm(U(j,:)-record(i,:));
end
min_value = min(temp_u);
if point_to_centre_distance < min_value
point_to_centre_distance = min_value;
x = i; % 记录
end
end
for i = 1:m
if (record(x,:) == data(i,:))
y = i;
break
end
end
U(N+1,:) = data(y,:); % 所找到的中心点加入集合
record(x, :) = []; % 所找到的中心点排除集合
N = N + 1;
end
end
%% k-means
for x=1:N
center(x,:)=U(x,:);
end
count = 1;
while true
new_center=zeros(N,n); %临时各类坐标
distence = zeros(1,N);
for x=1:m
for y=1:N
distence(y)=norm(data(x,:)-center(y,:));
end
[~, col]=min(distence);%求最小的距离
pattern(x,3)=col;
end
temp = [];
for t = 1:N
temp = pattern(find(pattern(:,n+1)==t),1:n);
ssum = zeros(1,size(temp,1));
for i = 1:size(temp,1)
for j = 1:size(temp,1)
ssum(i) = ssum(i)+norm(temp(i,:)-temp(j,:));
end
end
[~,col] = min(ssum);
new_center(t,:) = temp(col,:);
end
k=0;%初始条件
for y = 1:N
if norm(new_center(y,:)-center(y,:))<1e-1 %结束条件之一
k=k+1;
end
end
if k==N
break;
else
center=new_center; %更新聚类中心
end
end
%评价指标:
index=pattern(:,3);
index=index';
[AR,RI,MI,HI]=RandIndex(c1,index)
NMI=nmi(c1,index)
% AR =
% 0.9670
% RI =
% 0.9876
% MI =
% 0.0124
% HI =
% 0.9753
% NMI =
% 0.9483
disp('---------------------------------')
%绘图
figure(2)
plot(data(index==1,1),data(index==1,2),'r*')
hold on
plot(data(index==2,1),data(index==2,2),'b*')
hold on
plot(data(index==3,1),data(index==3,2),'g*')
hold on
plot(data(index==4,1),data(index==4,2),'y*')
title('自编写Kmeans算法图')
%% 系统自带的kmeans聚类算法
[Idx,C,sumD,D]=kmeans(data,k);
Idx=Idx';
%评价指标
[AR,RI,MI,HI]=RandIndex(c1,Idx)
NMI=nmi(c1,Idx)
%绘图
figure(3)
plot(data(Idx==1,1),data(Idx==1,2),'r*')
hold on
plot(data(Idx==2,1),data(Idx==2,2),'b*')
hold on
plot(data(Idx==3,1),data(Idx==3,2),'g*')
hold on
plot(data(Idx==4,1),data(Idx==4,2),'y*')
title('matlab自带Kmeans算法图')
matlab实现聚类算法
于 2021-04-14 22:20:00 首次发布
这篇博客介绍了如何实现自定义的K-Means聚类算法,并将其与MATLAB内置的K-Means算法进行对比。通过加载名为'simulate2020625'的数据集,博主展示了实际聚类图,并计算了多种评价指标,如AR、RI、MI、HI和NMI。结果显示,自编写的算法与系统自带的算法在聚类效果上进行了比较,提供了对两种算法性能的理解。
摘要由CSDN通过智能技术生成