教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 互联网资料 >

数据聚类实验报告(附代码)(2)

来源:网络收集 时间:2026-04-08
导读: 0.333 0.624 0.051 0.043 0.667 0.459 0.627 0.584 0.557 0.584 0.780 0.957 0.027 0.416 0.051 0.043 0.361 0.416 0.525 0.498 0.471 0.416 0.643 0.710 0.196 0.624 0.051 0.082 0.333 0.251 0.576 0.459 0.667 0

0.333 0.624 0.051 0.043 0.667 0.459 0.627 0.584 0.557 0.584 0.780 0.957 0.027 0.416 0.051 0.043 0.361 0.416 0.525 0.498 0.471 0.416 0.643 0.710 0.196 0.624 0.051 0.082 0.333 0.251 0.576 0.459 0.667 0.459 0.780 0.957 0.027 0.502 0.051 0.043 0.416 0.251 0.510 0.459 0.416 0.290 0.694 0.749 0.224 0.749 0.153 0.125 0.361 0.290 0.541 0.498 0.667 0.541 0.796 1.000 0.224 0.749 0.102 0.043 0.388 0.376 0.541 0.498 0.557 0.208 0.678 0.749 0.278 0.710 0.086 0.043 0.224 0.208 0.337 0.416 0.529 0.584 0.745 0.918 0.165 0.416 0.067 0.043 0.584 0.502 0.592 0.584 0.416 0.290 0.694 0.749 0.082 0.459 0.086 0.043 0.333 0.125 0.510 0.498 0.557 0.376 0.780 0.710 0.306 0.792 0.118 0.125 0.388 0.333 0.592 0.498 0.918 0.416 0.949 0.831 0.196 0.584 0.086 0.043 0.165 0.169 0.388 0.376 0.835 0.376 0.898 0.710 0.165 0.459 0.086 0.000 0.251 0.290 0.490 0.541 0.804 0.667 0.863 1.000 0.137 0.584 0.102 0.043 0.443 0.416 0.541 0.584 0.584 0.290 0.729 0.749 0.000 0.416 0.016 0.000 0.498 0.376 0.627 0.541 0.388 0.208 0.678 0.792 0.388 1.000 0.086 0.125 0.667 0.459 0.576 0.541

0.584 0.502 0.729 0.918 0.224 0.624 0.067 0.082 0.416 0.290 0.525 0.376 0.945 0.749 0.965 0.875 0.224 0.749 0.086 0.082 0.361 0.208 0.490 0.416 0.471 0.082 0.678 0.584 0.224 0.710 0.086 0.125 0.498 0.333 0.510 0.498 0.361 0.333 0.663 0.792 0.224 0.541 0.118 0.165 0.498 0.333 0.627 0.459 0.557 0.290 0.663 0.710 0.196 0.416 0.102 0.043 0.639 0.416 0.576 0.541 0.804 0.502 0.847 0.710 0.251 0.624 0.086 0.043 0.667 0.416 0.678 0.667 0.498 0.416 0.510 0.710 0.110 0.502 0.102 0.043 0.388 0.251 0.424 0.376 0.804 0.416 0.812 0.624 0.306 0.584 0.086 0.125 0.333 0.169 0.459 0.376 1.000 0.749 0.914 0.792 0.333 0.918 0.067 0.043 0.471 0.290 0.694 0.624 0.557 0.333 0.694 0.584 0.196 0.502 0.035 0.043 0.471 0.584 0.592 0.624 0.945 0.416 0.863 0.918 0.165 0.667 0.067 0.000 0.557 0.125 0.576 0.498 0.584 0.459 0.761 0.710 0.224 0.584 0.086 0.043 0.333 0.208 0.510 0.498 0.722 0.459 0.745 0.831 0.055 0.125 0.051 0.082 0.498 0.416 0.612 0.541 0.722 0.459 0.694 0.918 0.196 0.624 0.102 0.208 0.196 0.125 0.388 0.376 0.694 0.502 0.831 0.918 0.137 0.416 0.067 0.082

0.388 0.416 0.541 0.459 0.667 0.416 0.714 0.918 0.082 0.502 0.067 0.043 0.529 0.376 0.561 0.498 0.612 0.416 0.714 0.792 0.196 0.541 0.067 0.043 0.388 0.333 0.525 0.498 0.443 0.416 0.694 0.710 ];

[n,d] = size(x);

bn=round(n/k*rand);%第一个随机数在前1/K的范围内 nc=[x(bn,:);x(2*bn,:);x(3*bn,:);];%初始聚类中心 %nc=[x(bn,:);x(2*bn,:);x(3*bn,:);x(4*bn,:);];% 4类 [cid,nr,centers] = kmeans(x,k,nc)%调用kmeans函数 for i=1:150 if cid(i)==1,

plot(x(i,1),x(i,2),'r*') % 显示第一类 hold on else if cid(i)==2,

plot(x(i,1),x(i,2),'b*') %显示第二类 hold on else

if cid(i)==3,

plot(x(i,1),x(i,2),'g*') %显示第三类 hold on %else

%if cid(i)==4,

%plot(x(i,1),x(i,2),'k*') %显示第四类 %hold on %end end end end end

strt=['红色*为第一类;蓝色*为第二类;绿色*为第三类;黑色*为第四类' ]; text(-4,-3.6,strt);

%kmeans.m主类

function [cid,nr,centers] = kmeans(x,k,nc) [n,d] = size(x); % 设置cid为分类结果显示矩阵

cid = zeros(1,n); oldcid = ones(1,n);

nr = zeros(1,k); maxgn= 100; iter = 1;

while iter < maxgn %计算每个数据到聚类中心的距离 for i = 1:n

dist = sum((repmat(x(i,:),k,1)-nc).^2,2); [m,ind] = min(dist); % 将当前聚类结果存入cid中 cid(i) = ind; end

for i = 1:k

%找到每一类的所有数据,计算他们的平均值,作为下次计算的聚类中心 ind = find(cid==i); nc(i,:) = mean(x(ind,:)); % 统计每一类的数据个数 nr(i) = length(ind); end

iter = iter + 1; end

maxiter = 2; iter = 1; move = 1;

while iter < maxiter & move ~= 0

move = 0; % 对所有的数据进行再次判断,寻求最佳聚类结果 for i = 1:n

dist = sum((repmat(x(i,:),k,1)-nc).^2,2); r = cid(i); % 将当前数据属于的类给r

dadj = nr./(nr+1).*dist'; % 计算调整后的距离 [m,ind] = min(dadj); % 早到该数据距哪个聚类中心最近 if ind ~= r % 如果不等则聚类中心移动 cid(i) = ind;%将新的聚类结果送给cid

ic = find(cid == ind);%重新计算调整当前类别的聚类中心 nc(ind,:) = mean(x(ic,:)); move = 1; end end

iter = iter+1; end

centers = nc; if move == 0

disp('No points were moved after the initial clustering procedure.') else

disp('Some points were moved after the initial clustering procedure.')

end

…… 此处隐藏:1906字,全部文档内容请下载后查看。喜欢就下载吧 ……
数据聚类实验报告(附代码)(2).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/442542.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)