聚类分析算法
聚类分析又称群分析,它是研究(样品或指标)分类问题的一种统计分析方法,同时也是数据挖掘的一个重要算法。聚类分析是由若干模式组成的,通常,模式是一个度量的向量,聚类分析以相似性为基础,在一个聚类中的模式之间比不在同一聚类中的模式之间具有更多的相似性。
对于聚类算法,大多数用SPSS软件实现,通常导入数据,并且选择聚类方法即可实现,本节借用MATLAB软件,基于14种不同的聚类分析方法,实现样品聚类。
14种聚类方法
(1)最长距离法
X=[16.21492 2000 -8.2 6.2;
15.7 970 2209 -20.6 1.9;
16.3 1260 2085 -17.3 2.8;
17.2 14221726 -9.5 4.6;
18.8 1874 1709 -4.9 8.0;
17.9 1698 1848 -4.5 7.5;
16.3 976 1239-4.6 5.6];
D=pdist(X,"euclid");
M=squareform(D);
Z=linkage(D,"complete");
H=dendrogram(Z);
xlabel("City");
ylabel("Scale");
C=cophenet(Z,D);
T=cluster(Z,3);
![MATLAB数据分析,基于14种不同的聚类分析方法](/attachment/paiming/202109/hwyzcudphjj6932.jpg)
Rw.hAo22.com
(2) 最短距离法
X=[16.21492 2000 -8.2 6.2;
15.7 970 2209 -20.6 1.9;
16.3 1260 2085 -17.3 2.8;
17.2 14221726 -9.5 4.6;
18.8 1874 1709 -4.9 8.0;
17.9 1698 1848 -4.5 7.5;
16.3 976 1239-4.6 5.6];
D=pdist(X,"euclid");
M=squareform(D);
Z=linkage(D,"single")
;H=dendrogram(Z);
xlabel("City");
ylabel("Scale");
C=cophenet(Z,D);
T=cluster(Z,"cutoff",0.8);
![MATLAB数据分析,基于14种不同的聚类分析方法](/attachment/paiming/202109/wdowzk0cgx46933.jpg)
Rw.hAo22.com
(3)综合聚类子程序
X=[16.21492 2000 -8.2 6.2;
15.7 970 2209 -20.6 1.9;
16.3 1260 2085 -17.3 2.8;
17.2 14221726 -9.5 4.6;
18.8 1874 1709 -4.9 8.0;
17.9 1698 1848 -4.5 7.5;
16.3 976 1239-4.6 5.6];
T=clusterdata(X,0.8);
Re=find(T=5)
(4)重心法&标准欧氏距离
S=["福冈";"合肥";"武汉";"长沙";"桂林";"温州";"成都"];
X=[16.21492 2000 -8.2 6.2;
15.7 970 2209 -20.6 1.9;
16.3 1260 2085 -17.3 2.8;
17.2 14221726 -9.5 4.6;
18.8 1874 1709 -4.9 8.0;
17.9 1698 1848 -4.5 7.5;
16.3 976 1239-4.6 5.6];
D=pdist(X,"seuclid");
M=squareform(D);
Z=linkage(D,"centroid");
H=dendrogram(Z,"labels",S);
xlabel("City");
ylabel("Scale");
C=cophenet(Z,D);
T=cluster(Z,3);
![MATLAB数据分析,基于14种不同的聚类分析方法](/attachment/paiming/202109/cyl1ypht25p6934.jpg)
Rw.hAo22.com
(5)重心法&欧氏距离平方
S=["福冈";"合肥";"武汉";"长沙";"桂林";"温州";"成都"];
X=[16.21492 2000 -8.2 6.2;
15.7 970 2209 -20.6 1.9;
16.3 1260 2085 -17.3 2.8;
17.2 14221726 -9.5 4.6;
18.8 1874 1709 -4.9 8.0;
17.9 1698 1848 -4.5 7.5;
16.3 976 1239-4.6 5.6];
D=pdist(X,"euclid");
D2=D.^2;
M=squareform(D2);
Z=linkage(D2,"centroid");
H=dendrogram(Z,"labels",S);
xlabel("City");
ylabel("Scale");
C=cophenet(Z,D2);
T=cluster(Z,3);
![MATLAB数据分析,基于14种不同的聚类分析方法](/attachment/paiming/202109/olrgcy3bg3d6935.jpg)
(6)重心法&精度加权距离
S=["福冈";"合肥";"武汉";"长沙";"桂林";"温州";"成都"];
X=[16.21492 2000 -8.2 6.2;
15.7 970 2209 -20.6 1.9;
16.3 1260 2085 -17.3 2.8;
17.2 14221726 -9.5 4.6;
18.8 1874 1709 -4.9 8.0;
17.9 1698 1848 -4.5 7.5;
16.3 976 1239-4.6 5.6];
[n,m]=size(X);
stdx=std(X);
X2=X./stdx(ones(n,1),:);
D=pdist(X2,"euclid");
M=squareform(D);
Z=linkage(D,"centroid");
H=dendrogram(Z,"labels",S);
xlabel("City");
ylabel("Scale");
C=cophenet(Z,D);
T=cluster(Z,3);
![MATLAB数据分析,基于14种不同的聚类分析方法](/attachment/paiming/202109/ea2u42jtxnw6936.jpg)
(7)最短距离法&基于主成分的标准欧式距离
S=["福冈";"合肥";"武汉";"长沙";"桂林";"温州";"成都"];
X=[16.21492 2000 -8.2 6.2;
15.7 970 2209 -20.6 1.9;
16.3 1260 2085 -17.3 2.8;
17.2 14221726 -9.5 4.6;
18.8 1874 1709 -4.9 8.0;
17.9 1698 1848 -4.5 7.5;
16.3 976 1239-4.6 5.6];
[E,score,eigen,T]=princomp(X);
D=pdist(score,"seuclid");
M=squareform(D);
Z=linkage(D,"single");
H=dendrogram(Z,"labels",S);
xlabel("City");
ylabel("Scale");
C=cophenet(Z,D);
T=cluster(Z,3);
![MATLAB数据分析,基于14种不同的聚类分析方法](/attachment/paiming/202109/bbwa3f2pbhs6937.jpg)
(8)平均法&标准欧式距离
S=["福冈";"合肥";"武汉";"长沙";"桂林";"温州";"成都"];
X=[16.21492 2000 -8.2 6.2;
15.7 970 2209 -20.6 1.9;
16.3 1260 2085 -17.3 2.8;
17.2 14221726 -9.5 4.6;
18.8 1874 1709 -4.9 8.0;
17.9 1698 1848 -4.5 7.5;
16.3 976 1239-4.6 5.6];
D=pdist(X,"seuclid");
M=squareform(D);
Z=linkage(D,"average");
H=dendrogram(Z,"labels",S);
xlabel("City");
ylabel("Scale");
C=cophenet(Z,D);
T=cluster(Z,3);
![MATLAB数据分析,基于14种不同的聚类分析方法](/attachment/paiming/202109/ojmqveg0dqc6938.jpg)
(9)权重法&标准欧式距离
S=["福冈";"合肥";"武汉";"长沙";"桂林";"温州";"成都"];
X=[16.21492 2000 -8.2 6.2;
15.7 970 2209 -20.6 1.9;
16.3 1260 2085 -17.3 2.8;
17.2 14221726 -9.5 4.6;
18.8 1874 1709 -4.9 8.0;
17.9 1698 1848 -4.5 7.5;
16.3 976 1239-4.6 5.6];
D=pdist(X,"seuclid");
M=squareform(D);
Z=linkage(D,"weighted");
H=dendrogram(Z,"labels",S);
xlabel("City");
ylabel("Scale");
C=cophenet(Z,D);
T=cluster(Z,3);
![MATLAB数据分析,基于14种不同的聚类分析方法](/attachment/paiming/202109/pmvu13i34uc6939.jpg)
Rw.hAo22.com
(10)最短距离法&马氏距离
S=["福冈";"合肥";"武汉";"长沙";"桂林";"温州";"成都"];
X=[16.21492 2000 -8.2 6.2;
15.7 970 2209 -20.6 1.9;
16.3 1260 2085 -17.3 2.8;
17.2 14221726 -9.5 4.6;
18.8 1874 1709 -4.9 8.0;
17.9 1698 1848 -4.5 7.5;
16.3 976 1239-4.6 5.6];
D=pdist(X,"mahal");M=squareform(D);Z=linkage(D,"single");H=dendrogram(Z,"labels",S);xlabel("City");ylabel("Scale");C=cophenet(Z,D);T=cluster(Z,3);
![MATLAB数据分析,基于14种不同的聚类分析方法](/attachment/paiming/202109/mjfxmfznv5f6940.jpg)
(11)重心法&标准化数据的的欧式距离
S=["福冈";"合肥";"武汉";"长沙";"桂林";"温州";"成都"];
X=[16.21492 2000 -8.2 6.2;
15.7 970 2209 -20.6 1.9;
16.3 1260 2085 -17.3 2.8;
17.2 14221726 -9.5 4.6;
18.8 1874 1709 -4.9 8.0;
17.9 1698 1848 -4.5 7.5;
16.3 976 1239-4.6 5.6];
[n,m]=size(X);
mv=mean(X);
st=std(X);
x=(X-mv(ones(n,1),:))./st(ones(n,1),:);
D=pdist(X,"euclid");
M=squareform(D);
Z=linkage(D,"centroid");
H=dendrogram(Z,"labels",S);
xlabel("City");
ylabel("Scale");
C=cophenet(Z,D);
T=cluster(Z,3);
![MATLAB数据分析,基于14种不同的聚类分析方法](/attachment/paiming/202109/erfwnot0is56941.jpg)
(12)最长距离法&欧式距离
S=["福冈";"合肥";"武汉";"长沙";"桂林";"温州";"成都"];
X=[16.21492 2000 -8.2 6.2;
15.7 970 2209 -20.6 1.9;
16.3 1260 2085 -17.3 2.8;
17.2 14221726 -9.5 4.6;
18.8 1874 1709 -4.9 8.0;
17.9 1698 1848 -4.5 7.5;
16.3 976 1239-4.6 5.6];
D=pdist(X,"euclid");
M=squareform(D);
Z=linkage(D,"complete");
[H tPerm]=dendrogram(Z,"labels",S);
xlabel("City");
ylabel("Scale");
C=cophenet(Z,D);
T=cluster(Z,3);
![MATLAB数据分析,基于14种不同的聚类分析方法](/attachment/paiming/202109/crusx134ltd6942.jpg)
(13)平均法&相似系数
S=["福冈";"合肥";"武汉";"长沙";"桂林";"温州";"成都"];
X=[16.21492 2000 -8.2 6.2;
15.7 970 2209 -20.6 1.9;
16.3 1260 2085 -17.3 2.8;
17.2 14221726 -9.5 4.6;
18.8 1874 1709 -4.9 8.0;
17.9 1698 1848 -4.5 7.5;
16.3 976 1239-4.6 5.6];
D=pdist(X,"cosine");
M=squareform(D);
Z=linkage(D,"centroid");
T=dendrogram(Z,"labels",S);
xlabel("City");
ylabel("Scale");
C=cophenet(Z,D);
T=cluster(Z,3);
![MATLAB数据分析,基于14种不同的聚类分析方法](/attachment/paiming/202109/ywfb0cj23jk6943.jpg)
Rw.hAo22.com
(14)最短距离法&基于主成分的标准欧式距离
S=["福冈";"合肥";"武汉";"长沙";"桂林";"温州";"成都"];
X=[16.21492 2000 -8.2 6.2;
15.7 970 2209 -20.6 1.9;
16.3 1260 2085 -17.3 2.8;
17.2 14221726 -9.5 4.6;
18.8 1874 1709 -4.9 8.0;
17.9 1698 1848 -4.5 7.5;
16.3 976 1239-4.6 5.6];
[E,score,eigen,T]=princomp(X);
PCA=[score(:,1),score(:,2)];
D=pdist(PCA,"seuclid");
M=squareform(D);
Z=linkage(D,"single");
H=dendrogram(Z,"labels",S);
xlabel("City");
ylabel("Scale");
C=cophenet(Z,D);
T=cluster(Z,3);
![MATLAB数据分析,基于14种不同的聚类分析方法](/attachment/paiming/202109/j2ieil1geca6944.jpg)