【MATLAB数学建模算法代码(七)之聚类分析】

注意:因为特殊原因,MATLAB数学建模算法代码就更新到这。
私信的人太多了,以后只更新算法原理和建模方法。防止******!
以下是今天内容
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',[]);

你可能感兴趣的:(MATLAB数学建模,算法,matlab,聚类)