matlab聚类算法实例(matlab聚类分析的应用案例)
聚类分析算法
聚类分析又称群分析,它是研究(样品或指标)分类问题的一种统计分析方法,同时也是数据挖掘的一个重要算法。聚类分析是由若干模式组成的,通常,模式是一个度量的向量,聚类分析以相似性为基础,在一个聚类中的模式之间比不在同一聚类中的模式之间具有更多的相似性。
对于聚类算法,大多数用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);
(2) 最短距离法
Rw。HAo22.coMX=[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);
(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)
Rw。HAo22.coM(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);
(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);
(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);
(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);
(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);
(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);
(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);
(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);
(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);
Rw。HAo22.coM(13)平均法&相似系数
Rw。HAo22.coMS=["福冈";"合肥";"武汉";"长沙";"桂林";"温州";"成都"];
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);
(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);
Rw。HAo22.coM.FJX97 { display:none; }
版权声明:如无特殊标注,文章均为本站原创,转载时请以链接形式注明文章出处。
评论