cluster函数matlab(如何利用matlab求r型聚类分析)

本文目录
- 如何利用matlab求r型聚类分析
- matlab 聚类分析kmeans和cluster的区别
- matlab作矩阵的聚类并做出图形
- matlab中的功能函数FCM如何使用
- matlab如何代码实现分裂层次聚类
- 怎样用matlab实现多维K-means聚类算法
- matlab 中cluster参数代表什么(我是菜鸟)
如何利用matlab求r型聚类分析
本文重点是展示如何用Matlab来进行聚类分析。如果有需要解答的问题请留言,笔者会尽其所能地回答。
内容
展示如何使用MATLAB进行聚类分析
生成随机二维分布图形,三个中心
K均值聚类
分层聚类
重新调用K均值法
将分类的结果展示出来
运用高斯混合分布模型进行聚类分析
通过AIC准则寻找最优的分类数
展示如何使用MATLAB进行聚类分析
分别运用分层聚类、K均值聚类以及高斯混合模型来进行分析,然后比较三者的结果
生成随机二维分布图形,三个中心
% 使用高斯分布(正态分布)
% 随机生成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’);
title(’研究样本散点分布图’)
K均值聚类
% 距离用传统欧式距离,分成两类
= kmeans(X,2,’dist’,’sqEuclidean’);
P2 = figure;clf;
= silhouette(X,cidx2,’sqeuclidean’);
从轮廓图上面看,第二类结果比较好,但是第一类有部分数据表现不佳。有相当部分的点落在0.8以下。
分层聚类
eucD = pdist(X,’euclidean’);
clustTreeEuc = linkage(eucD,’average’);
cophenet(clustTreeEuc,eucD);
P3 = figure;clf;
= dendrogram(clustTreeEuc,20);
set(gca,’TickDir’,’out’,’TickLength’,);
可以选择dendrogram显示的结点数目,这里选择20 。结果显示可能可以分成三类
重新调用K均值法
改为分成三类
= kmeans(X,3,’dist’,’sqEuclidean’);
P4 = figure;clf;
= silhouette(X,cidx3,’sqeuclidean’);
图上看,比前面的结果略有改善。
将分类的结果展示出来
P5 = figure;clf
ptsymb = {’bo’,’ro’,’go’,’,mo’,’c+’};
MarkFace = {};
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
运用高斯混合分布模型进行聚类分析
分别用分布图、热能图和概率图展示结果 等高线
% 等高线
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,);
hold off
P7 = figure;clf
scatter(X(:,1),X(:,2),10,’ro’);
hold on
ezsurf(@(x,y) pdf(gm,);
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
= 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’);
通过AIC准则寻找最优的分类数
高斯混合模型法的最大好处是给出分类好坏的标准
AIC = zeros(1,4);
NlogL = AIC;
GM = cell(1,4);
for k = 1:4
GM{k} = gmdistribution.fit(X,k);
AIC(k)= GM{k}.AIC;
NlogL(k) = GM{k}.NlogL;
end
= min(AIC);
按AIC准则给出的最优分类数为: 3 对应的AIC值为: 8647.63
后记
(1)pluskid指出K均值算法的初值对结果很重要,但是在运行时还没有发现类似的结果。也许Mathworks对该算法进行过优化。有时间会仔细研究下代码,将结果放上来。
转载仅供参考,版权属于原作者
matlab 聚类分析kmeans和cluster的区别
kmeans是K均值聚类
cluster是层次聚类
从总体思想上k均值是由上到下的,他是在你给定所分的类数后,保证这K类之间获得最大的划分。而层次聚类是由下到上的,它把每一个个元素视为一类,然后距离最短的两类合为一类,逐渐合并合所有元素并成一个大类。
K均值聚类保证了你所确定的K有着最好的划分效果,但是可能不符合数据自身分类特征,层次聚类的树状图能看到数据分类过程和分类距离,但是未必满足你所需要的K
matlab作矩阵的聚类并做出图形
%生成随机数据
clear;clc;
a=10*(1:2:9);
b=;
idx=randperm(200);
for n=1:5
X(idx((b(n)+1):b(n+1)),:)=unifrnd(a(n),a(n)+10,b(n+1)-b(n),3);
end
%聚类
Z=clusterdata(X,’maxclust’,5);
%绘图
for n=1:5
Y(n,:)=mean(X(Z==n,:));
end
scatter3(X(:,1),X(:,2),X(:,3),10,Z);
hold on;
plot3(Y(:,1),Y(:,2),Y(:,3),’rp’,’markerfacecolor’,’r’,’markersize’,10)
matlab中的功能函数FCM如何使用
模糊C均值聚类算法,可将输入的数据集data聚为指定的cluster_n类
【函数描述】
语法格式
= FCM(data, cluster_n, options)
用法:
1. = FCM(Data,N_cluster,options);
2. = FCM(Data,N_cluster);
输入变量
data ---- n*m矩阵,表示n个样本,每个样本具有m维特征值
cluster_n ---- 标量,表示聚合中心数目,即类别数
options ---- 4*1列向量,其中
options(1): 隶属度矩阵U的指数,》1(缺省值: 2.0)
options(2): 最大迭代次数(缺省值: 100)
options(3): 隶属度最小变化量,迭代终止条件(缺省值: 1e-5)
options(4): 每次迭代是否输出信息标志(缺省值: 0)
输出变量
center ---- 聚类中心
U ---- 隶属度矩阵
obj_fcn ---- 目标函数值
matlab如何代码实现分裂层次聚类
近需要用到层次聚类,发现在Matlab上很容易实现,下面是代码加详细注释
view plain copy
clear all
clc
close all
mdist=input(’输入坐标文件名字\n’);
disp(’读取数据坐标’)
%获取坐标
%文件为二维的坐标,第一列为x轴坐标,第二列为y轴坐标
xx=load(mdist);
%获取数据的个数存至number
=size(xx);
%获取距离矩阵,第二参数指定距离计算方法
%可选参数有’euclidean’:欧氏距离(默认);’seuclidean’:标准化欧氏距离;
%’mahalanobis’:马氏距离;’cityblock’:布洛克距离;’minkowski’:明可夫斯基距离;
%’cosine’:余弦距离 ’correlation’:相关性 ’hamming’:汉明距离 ’jaccard’:Jaccard相似度
%’chebychev’:Chebychev距离。
yy=pdist(xx,’euclidean’);
%获取聚类,第二参数指定层次聚类方式
%’single’:单连通,最短距离法(默认);’complete’:全连通,最长距离法;’average’:未加权平均距离法;
%’weighted’: 加权平均法;’centroid’: 质心距离法;’median’:加权质心距离法;’ward’:内平方距离法(最小方差算法)
zz=linkage(yy,’single’);
%指定获取簇类个数
Ncluster=input(’输入类个数\n’);
%获取指定Ncluster个数的聚类结果
c = cluster( zz,’maxclust’, Ncluster );
%获取绘图所需颜色
%需要用到linspecer.m文件
%下载地址
***隐藏网址***
if(Ncluster》12)
Color = linspecer( Ncluster );
else
Color = linspecer( Ncluster, ’qualitative’ );
end
for i=1:Ncluster
for j = 1:number
if(c(j) == i)
hold on
plot(xx(j,1),xx(j,2),’o’,’MarkerFaceColor’,Color(i,:),’MarkerEdgeColor’,Color(i,:))
end
end
end
怎样用matlab实现多维K-means聚类算法
直接用kmeans函数。。。
idx = kmeans(X,k)
idx = kmeans(X,k,Name,Value)
= kmeans(___)
= kmeans(___)
= kmeans(___)
idx = kmeans(X,k) performs k-means clustering to partition the observations of the n-by-p data matrix X into k clusters, and returns an n-by-1 vector (idx) containing cluster indices of each observation. Rows of X correspond to points and columns correspond to variables.
By default, kmeans uses the squared Euclidean distance measure and the k-means++ algorithm for cluster center initialization.
example
idx = kmeans(X,k,Name,Value) returns the cluster indices with additional options specified by one or more Name,Value pair arguments.
For example, specify the cosine distance, the number of times to repeat the clustering using new initial values, or to use parallel computing.
example
= kmeans(___) returns the k cluster centroid locations in the k-by-p matrix C.
example
= kmeans(___) returns the within-cluster sums of point-to-centroid distances in the k-by-1 vector sumd.
example
= kmeans(___) returns distances from each point to every centroid in the n-by-k matrix D.
matlab 中cluster参数代表什么(我是菜鸟)
T = cluster(Z,’cutoff’,c) constructs clusters from the hierarchical cluster tree, Z, as generated by the linkage function. Z is a matrix of size (m – 1)-by-3, where m is the number of observations in the original data. c is a threshold for cutting Z into clusters. Clusters are formed when a node and all of its subnodes have inconsistent value less than c. All leaves at or below the node are grouped into a cluster. t is a vector of size m containing the cluster assignments of each observation.

本文相关文章:
plot函数详解(matlab中ezplot和plot, fplot这3个绘图命令的区别是什么)
2026年7月9日 14:10
matlab的meshgrid(matlab 中的meshgid是什么意思)
2026年6月29日 01:50
更多文章:
avantage怎么设置为中文(Avantage安装丢失GIT)
2026年10月15日 03:00
activex控件未注册怎么办(登陆视频监控网页时提示activex控件未注册,怎么解决)
2026年10月14日 20:30
excel连接sql数据库教程(excel怎么连接sql数据库)
2026年10月14日 19:40





