全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
共享一个在数据挖掘课程中作为示例使用的凝聚层次聚类算法源码,供大家学习交流使用时将源码复制进一个新的function并更改m文件的文件名为agnes即可-2016年4月10日以下为程序的matlab源码:function cluster=agnes(pointset,targetclusternum,method)% 凝聚层次聚类算法% 输入:点集pointset、目标簇数targetclusternum、簇间差异度度量方式method% 点集pointset为n*m矩阵,包含n个点,每个点有m个属性% 目标簇数targetclusternum为一个整数,0targetclusternumn% method为字符串,对应不同距离度量方式:% method=min:最小距离度量;method=max:最大距离度量;method=mean:均值距离度量;method=avg:平均距离度量;% 输出:cluster为长度为n的向量,表示各点所对应簇的类别标记% 调用方式示例:cluster=agnes(pointset,3,max);% 表示将pointset使用最大距离度量方式聚为3个类,将通过cluster变量返回类标记 pointnum=size(pointset,1);cluster=1:pointnum; %每个点对应簇标记 i=0;while true i=i+1; unicluster=unique(cluster);clusternum=size(unicluster,2); %当前簇数目。初始默认每个点为单独的一个簇 if clusternum=targetclusternum%若聚类数满足结束条件则算法结束 break; end %计算簇间差异度 clusterdist=zeros(clusternum); if strcmp(method,mean)=1%计算各簇间差异度:平均距离 clustermean=; for c=1:clusternum%计算各簇均值 unicluster=unique(cluster); subcluster=pointset(cluster=unicluster(c),:); clustermean=clustermean;unicluster(c),mean(subcluster,1); end for d=1:size(clustermean,1) %计算各簇间距离 clusterdist(:,d)=sqrt(sum(clustermean(:,2:end)-repmat(clustermean(d,2:end),size(clustermean,1),1).2,2); end clusterdist=clusterdist+eye(size(clusterdist,1)*max(max(clusterdist)*100; %将对角线设为最大值 elseif strcmp(method,min)=1%计算各簇间差异度:最小距离 for c1=1:clusternum for c2=c1+1:clusternum subpointset1=pointset(cluster=unicluster(c1),:); subpointset2=pointset(cluster=unicluster(c2),:); subpointset1expend=; for c3=1:size(subpointset1,1) subpointset1expend=subpointset1expend;repmat(subpointset1(c3,:),size(subpointset2,1),1); end subpointset2expend=repmat(subpointset2,size(subpointset1,1),1); dist=sqrt(sum(subpointset1expend-subpointset2expend).2,2); clusterdist(c1,c2)=min(dist); %取最小值 end end clusterdist=clusterdist+clusterdist; %将三角阵转换为对称阵 clusterdist=clusterdist+eye(size(clusterdist,1)*max(max(clusterdist)*100; %将对角线设为最大值 elseif strcmp(method,max)=1%计算各簇间差异度:最大距离 for c1=1:clusternum for c2=c1+1:clusternum subpointset1=pointset(cluster=unicluster(c1),:); subpointset2=pointset(cluster=unicluster(c2),:); subpointset1expend=; for c3=1:size(subpointset1,1) subpointset1expend=subpointset1expend;repmat(subpointset1(c3,:),size(subpointset2,1),1); end subpointset2expend=repmat(subpointset2,size(subpointset1,1),1); dist=sqrt(sum(subpointset1expend-subpointset2expend).2,2); clusterdist(c1,c2)=max(dist); %取最大值 end end clusterdist=clusterdist+clusterdist; clusterdist=clusterdist+eye(size(clusterdist,1)*max(max(clusterdist)*100; elseif strcmp(method,avg)=1%计算各簇间差异度:均值距离 for c1=1:clusternum for c2=c1+1:clusternum subpointset1=pointset(cluster=unicluster(c1),:); subpointset2=pointset(cluster=unicluster(c2),:); subpointset1expend=; for c3=1:size(subpointset1,1) subpointset1expend=subpointset1expend;repmat(subpointset1(c3,:),size(subpointset2,1),1); end subpointset2expend=repmat(subpointset2,size(subpointset1,1),1); dist=sqrt(sum(subpointset1expend-subpointset2expend).2,2); clusterdist(c1,c2)=sum(dist)./size(dist,1); %取均值 end end clusterdist=clusterdist+clusterdist; clusterdist=clusterdist+eye(size(clusterdist,1)*max(max(clusterdist)*100; end %合并相异度最小的两个簇 rows,cols=find
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年有子女无财产离婚协议参考模板(四篇)
- 2024年工矿产品购销合同标准版(5篇)
- 2024年委托管理协议简易版(三篇)
- 2024年彩钢工程承包合同标准范本(二篇)
- 2024年二手房屋租赁协议范文(二篇)
- 2024年四川省农业订单合同(二篇)
- 2024年雇佣保姆合同官方版(2篇)
- 2024年租赁安全协议(3篇)
- 2024年有关解除租赁合同简易版(二篇)
- 2024年诊所合伙协议(二篇)
- FZ/T 70017-2022针织服装印花质量通用技术要求
- JJF 1993-2022天然气能量计量技术规范
- MT/T 199-1996煤矿用液压钻车通用技术条件
- GB/T 39015.2-2020道路车辆牵引车与半挂车之间机械连接互换性第2部分:低牵引座半挂牵引车与大容积半挂车
- 修理厂汛期安全应急预案(7篇)
- 三年级美术下册《可爱的汽车》课件
- 2022年中国文化传媒集团有限公司招聘笔试试题及答案解析
- 小学少先队中队辅导员聘书格式
- DBJ33∕T 1272-2022 城市道路养护管理评价标准
- 城市绿地分类标准
- 中医保健温通灸课件
评论
0/150
提交评论