%function [result,c,s,index,label]=kpam(data,k)
%result表示聚类结果
%c表示聚类最后的中心点
%index表示随机排列的行号
%label表示数据每一行属于第几个中心点
%随机选着k个中心点
clc;clear all;
data=[1702.8 1639.79 2068.74
1877.93 1860.96 1975.3
867.81 2334.68 2535.1
1831.49 1713.11 1604.68
460.69 3274.77 2172.99
2374.98 3346.98 975.31
2271.89 3482.97 946.7
1783.64 1597.99 2261.31
198.83 3250.45 2445.08
1494.63 2072.59 2550.51
1597.03 1921.52 2126.76
1598.93 1921.08 1623.33
1243.13 1814.07 3441.07
2336.31 2640.26 1599.63
354 3300.12 2373.61
2144.47 2501.62 591.51
426.31 3105.29 2057.8
1507.13 1556.89 1954.51
343.07 3271.72 2036.94
2201.94 3196.22 935.53
2232.43 3077.87 1298.87
1580.1 1752.07 2463.04
1962.4 1594.97 1835.95
1495.18 1957.44 3498.02
1125.17 1594.39 2937.73
24.22 3447.31 2145.01
1269.07 1910.72 2701.97
1802.07 1725.81 1966.35
1817.36 1926.4 2328.79
1860.45 1782.88 1875.13];
k=4;
[N,n]=size(data);
index=randperm(N);%打乱N个数的顺序
v=data(index(1:k),:);%初始化速度%
for t=1:100
%指派每个剩余的对象个离它最近的中心点所代表的簇
% if k==1
% for j=1:N
% label(j)=1;
% end
% else
for i=1:k
label(index(i))=i;
end
for j=k+1:N
for i=1:k
dist(:,i)=sqrt(sum((data(index(j),:)-v(i,:)).^2));%计算距离%
end
[m,l]=min(dist');%选取距离最小%
label(index(j))=l;
end
end
for i=1:k
c(i,:)=v(i,:);
end
%所有非中心点被选择过,所有的中心点被选择过
for i=1:k
for h=k+1:N
for j=1:N
c(i,:)=data(index(h),:);
dist1=sqrt(sum((data(j,:)-c(i,:)).^2));%计算距离%
for z=1:k
dist2(z)= sqrt(sum((data(j,:)-c(z,:)).^2));%计算距离%
end
for y=1:k
dist4(y)=sqrt(sum((data(j,:)-v(y,:)).^2));%计算距离%
end
dist3= sqrt(sum((data(j,:)-v(i,:)).^2));%计算距离%
if label(j)==i
if dist1==min(dist2)
cjih(j,:)=dist1-dist3;
else
cjih(j,:)=min(dist2)-dist3;
end
else
if dist1==min(dist4)
cjih(j,:)=dist1-min(dist4);
else
cjih(j,:)=0;
end
end
c(i,:)=v(i,:);
%s1(j,:)=cjih(j,:); ?
end
%一个非中心点代替一个中心点的总代价s
s((h-k),:,i)=sum(cjih(:,:),1);
end
end
% if min(min(s))==0
for i=1:k
for h=k+1:N
if s((h-k),:,i)==min(min(s))
s((h-k),:,i)=1;
end
end
end
% end
%If 在S中的所有非中心点代替所有中心点后的计算出总代价有小于0的存在,then找出S中的用非中心点替代中心点后代价最小的一个,并用该非中心点替代对应的中心点,形成一个新的k个中心点的集合
if min(min(s))<0
for i=1:k
for h=k+1:N
if s((h-k),:,i)==min(min(s))
v(i,:)=data(index(h),:);
end
end
if data(index(i),:)~=data(index(h),:)
if v(i,:)==data(index(h),:)
end
end
end
a=index(i);
b=index(h);
index(i)=b;
index(h)=a;
end
% 所有的s都大于0则聚类完成
if min(min(s))>0
end
%end
for i=1:k
for j=1:N
if label(j)==i
result(j,:,i)=data(j,:)
% line(result(:,1,1),result(:,2,1),result(:,3,1),'linestyle','none','marker','*','color','g');
% line(result(:,1,2),result(:,2,2),result(:,3,2),'linestyle','none','marker','o','color','b');
% line(result(:,1,3),result(:,2,3),result(:,3,3),'linestyle','none','marker','+','color','r');
% line(result(:,1,4),result(:,2,4),result(:,3,4),'linestyle','none','marker','@','color','y');
% line(data(:,1),data(:,2),data(:,3),'linestyle','none','marker','*','color','r');
% line(data(:,1),data(index3,2),data(index3,3),'linestyle','none','marker','+','color','b');
% line(data(index4,1),data(index4,2),data(index4,3),'linestyle','none','marker','+','color','y');
%title('C均值聚类分析图');
%xlabel('第一特征坐标');
%ylabel('第二特征坐标');
%zlabel('第三特征坐标');
end
end
end
figure
plot3(result(:,1,1),result(:,2,1),result(:,3,1),'*');
hold on;
plot3(result(:,1,2),result(:,2,2),result(:,3,2),'*');
plot3(result(:,1,3),result(:,2,3),result(:,3,3),'*');
plot3(result(:,1,4),result(:,2,4),result(:,3,4),'*');
grid on;
line(result(:,1,1),result(:,2,1),result(:,3,1),'linestyle','none','marker','*','color','y');
line(result(:,1,2),result(:,2,2),result(:,3,2),'linestyle','none','marker','*','color','b');
line(result(:,1,3),result(:,2,3),result(:,3,3),'linestyle','none','marker','*','color','g');
line(result(:,1,4),result(:,2,4),result(:,3,4),'linestyle','none','marker','*','color','r');
聚类PAM----MATLAB
于 2022-04-16 14:47:23 首次发布