matlab——聚類分析

聚類分析主要過程

(1)將數據展繪
% 隨機生成3箇中心以及標準差
s = rng(5,'v5normal');
mu = round((rand(3,2)-0.5)*19)+1;
sigma = round(rand(3,2)*40)/10+1;
X = [mvnrnd(mu(1,:),sigma(1,:),200);
mvnrnd(mu(2,:),sigma(2,:),300);
mvnrnd(mu(3,:),sigma(3,:),400)];
% 作圖
P1 = figure;clf;
scatter(X(:,1),X(:,2),10,'ro');
(2)利用不同的算法進行帶入分析

1 高斯混合聚類代碼

% 等高線
options = statset('Display','off');
gm = gmdistribution.fit(X,3,'Options',options);
P6 = figure;clf
scatter(X(:,1),X(:,2),10,'ro');
hold on
ezcontour(@(x,y) pdf(gm,[x,y]),[-15 15],[-15 10]);
hold off
P7 = figure;clf
scatter(X(:,1),X(:,2),10,'ro');
hold on
ezsurf(@(x,y) pdf(gm,[x,y]),[-15 15],[-15 10]);
hold off
view(33,24)
cluster1 = (cidx3 == 1);
cluster3 = (cidx3 == 2);
% 通過觀察,K均值方法的第二類是gm的第三類
cluster2 = (cidx3 == 3);
% 計算分類概率
P = posterior(gm,X);
P8 = figure;clf
plot3(X(cluster1,1),X(cluster1,2),P(cluster1,1),'r.')
grid on;hold on
plot3(X(cluster2,1),X(cluster2,2),P(cluster2,2),'bo')
plot3(X(cluster3,1),X(cluster3,2),P(cluster3,3),'g*')
legend('第 1 類','第 2 類','第 3 類','Location','NW')
clrmap = jet(80); colormap(clrmap(9:72,:))
ylabel(colorbar,'Component 1 Posterior Probability')
view(-45,20);
% 第三類點部分概率值較低,可能需要其他數據來進行分析。
% 概率圖
P9 = figure;clf
[~,order] = sort(P(:,1));
plot(1:size(X,1),P(order,1),'r-',1:size(X,1),P(order,2),'b-',1:size(X,1),P(order,3),'y-');
legend({'Cluster 1 Score' 'Cluster 2 Score' 'Cluster 3 Score'},'location','NW');
ylabel('Cluster Membership Score');
xlabel('Point Ranking');

2 K均值聚類算法

[cidx3,cmeans3,sumd3,D3] = kmeans(X,3,'dist','sqEuclidean');
P4 = figure;clf;
[silh3,h3] = silhouette(X,cidx3,'sqeuclidean');
P5 = figure;clf
ptsymb = {'bo','ro','go',',mo','c+'};
MarkFace = {[0 0 1],[.8 0 0],[0 .5 0]};
hold on
for i =1:3
clust = find(cidx3 == i);
plot(X(clust,1),X(clust,2),ptsymb{i},'MarkerSize',3,'MarkerFace',MarkFace{i},'MarkerEdgeColor','black');
plot(cmeans3(i,1),cmeans3(i,2),ptsymb{i},'MarkerSize',10,'MarkerFace',MarkFace{i});
end
hold off

3 分層聚類算法代碼

eucD = pdist(X,'euclidean');
clustTreeEuc = linkage(eucD,'average');
cophenet(clustTreeEuc,eucD);
P3 = figure;clf;
[h,nodes] =? dendrogram(clustTreeEuc,20);
set(gca,'TickDir','out','TickLength',[.002 0],'XTickLabel',[]);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章