• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[matlab] 23.matlab自带kmeans函数 实现聚类

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
clc,clear all;
point=[1.40000000000000,0.200000000000000;1.40000000000000,0.200000000000000;1.30000000000000,0.200000000000000;1.50000000000000,0.200000000000000;1.40000000000000,0.200000000000000;1.70000000000000,0.400000000000000;1.40000000000000,0.300000000000000;1.50000000000000,0.200000000000000;1.40000000000000,0.200000000000000;1.50000000000000,0.100000000000000;1.50000000000000,0.200000000000000;1.60000000000000,0.200000000000000;1.40000000000000,0.100000000000000;1.10000000000000,0.100000000000000;1.20000000000000,0.200000000000000;1.50000000000000,0.400000000000000;1.30000000000000,0.400000000000000;1.40000000000000,0.300000000000000;1.70000000000000,0.300000000000000;1.50000000000000,0.300000000000000;1.70000000000000,0.200000000000000;1.50000000000000,0.400000000000000;1,0.200000000000000;1.70000000000000,0.500000000000000;1.90000000000000,0.200000000000000;1.60000000000000,0.200000000000000;1.60000000000000,0.400000000000000;1.50000000000000,0.200000000000000;1.40000000000000,0.200000000000000;1.60000000000000,0.200000000000000;1.60000000000000,0.200000000000000;1.50000000000000,0.400000000000000;1.50000000000000,0.100000000000000;1.40000000000000,0.200000000000000;1.50000000000000,0.200000000000000;1.20000000000000,0.200000000000000;1.30000000000000,0.200000000000000;1.40000000000000,0.100000000000000;1.30000000000000,0.200000000000000;1.50000000000000,0.200000000000000;1.30000000000000,0.300000000000000;1.30000000000000,0.300000000000000;1.30000000000000,0.200000000000000;1.60000000000000,0.600000000000000;1.90000000000000,0.400000000000000;1.40000000000000,0.300000000000000;1.60000000000000,0.200000000000000;1.40000000000000,0.200000000000000;1.50000000000000,0.200000000000000;1.40000000000000,0.200000000000000;4.70000000000000,1.40000000000000;4.50000000000000,1.50000000000000;4.90000000000000,1.50000000000000;4,1.30000000000000;4.60000000000000,1.50000000000000;4.50000000000000,1.30000000000000;4.70000000000000,1.60000000000000;3.30000000000000,1;4.60000000000000,1.30000000000000;3.90000000000000,1.40000000000000;3.50000000000000,1;4.20000000000000,1.50000000000000;4,1;4.70000000000000,1.40000000000000;3.60000000000000,1.30000000000000;4.40000000000000,1.40000000000000;4.50000000000000,1.50000000000000;4.10000000000000,1;4.50000000000000,1.50000000000000;3.90000000000000,1.10000000000000;4.80000000000000,1.80000000000000;4,1.30000000000000;4.90000000000000,1.50000000000000;4.70000000000000,1.20000000000000;4.30000000000000,1.30000000000000;4.40000000000000,1.40000000000000;4.80000000000000,1.40000000000000;5,1.70000000000000;4.50000000000000,1.50000000000000;3.50000000000000,1;3.80000000000000,1.10000000000000;3.70000000000000,1;3.90000000000000,1.20000000000000;5.10000000000000,1.60000000000000;4.50000000000000,1.50000000000000;4.50000000000000,1.60000000000000;4.70000000000000,1.50000000000000;4.40000000000000,1.30000000000000;4.10000000000000,1.30000000000000;4,1.30000000000000;4.40000000000000,1.20000000000000;4.60000000000000,1.40000000000000;4,1.20000000000000;3.30000000000000,1;4.20000000000000,1.30000000000000;4.20000000000000,1.20000000000000;4.20000000000000,1.30000000000000;4.30000000000000,1.30000000000000;3,1.10000000000000;4.10000000000000,1.30000000000000;6,2.50000000000000;5.10000000000000,1.90000000000000;5.90000000000000,2.10000000000000;5.60000000000000,1.80000000000000;5.80000000000000,2.20000000000000;6.60000000000000,2.10000000000000;4.50000000000000,1.70000000000000;6.30000000000000,1.80000000000000;5.80000000000000,1.80000000000000;6.10000000000000,2.50000000000000;5.10000000000000,2;5.30000000000000,1.90000000000000;5.50000000000000,2.10000000000000;5,2;5.10000000000000,2.40000000000000;5.30000000000000,2.30000000000000;5.50000000000000,1.80000000000000;6.70000000000000,2.20000000000000;6.90000000000000,2.30000000000000;5,1.50000000000000;5.70000000000000,2.30000000000000;4.90000000000000,2;6.70000000000000,2;4.90000000000000,1.80000000000000;5.70000000000000,2.10000000000000;6,1.80000000000000;4.80000000000000,1.80000000000000;4.90000000000000,1.80000000000000;5.60000000000000,2.10000000000000;5.80000000000000,1.60000000000000;6.10000000000000,1.90000000000000;6.40000000000000,2;5.60000000000000,2.20000000000000;5.10000000000000,1.50000000000000;5.60000000000000,1.40000000000000;6.10000000000000,2.30000000000000;5.60000000000000,2.40000000000000;5.50000000000000,1.80000000000000;4.80000000000000,1.80000000000000;5.40000000000000,2.10000000000000;5.60000000000000,2.40000000000000;5.10000000000000,2.30000000000000;5.10000000000000,1.90000000000000;5.90000000000000,2.30000000000000;5.70000000000000,2.50000000000000;5.20000000000000,2.30000000000000;5,1.90000000000000;5.20000000000000,2;5.40000000000000,2.30000000000000;5.10000000000000,1.80000000000000];


figure;
plot(point(:,1),point(:,2),'ks','MarkerSize',5);
title 'Fisher''s Iris Data';
xlabel 'Petal Lengths (cm)'; 
ylabel 'Petal Widths (cm)';

rng(1); % For reproducibility
[idx,C] = kmeans(point,3);

x1 = min(point(:,1)):0.01:max(point(:,1));
x2 = min(point(:,2)):0.01:max(point(:,2));
[x1G,x2G] = meshgrid(x1,x2);
XGrid = [x1G(:),x2G(:)]; % Defines a fine grid on the plot

idx2Region = kmeans(XGrid,3,'MaxIter',1,'Start',C);

figure;
gscatter(XGrid(:,1),XGrid(:,2),idx2Region,[0,0.75,0.75;0.75,0,0.75;0.75,0.75,0],'..');
hold on;
plot(point(:,1),point(:,2),'ks','MarkerSize',5);
title 'Fisher''s Iris Data';
xlabel 'Petal Lengths (cm)';
ylabel 'Petal Widths (cm)'; 
legend('Region 1','Region 2','Region 3','Data','Location','SouthEast');
hold off;
kmeans

 

更多细节参考 k-means clustering

 

 

 

rng default; % For reproducibility
X = [randn(100,2)*0.75+ones(100,2);
    randn(100,2)*0.5-ones(100,2)];

figure(1);
plot(X(:,1),X(:,2),'k.','MarkerSize',12);
title 'Randomly Generated Data';

opts = statset('Display','final');
[idx,C] = kmeans(X,2,'Distance','cityblock','Replicates',5,'Options',opts); %这里可以改聚类数量

figure(2);
plot(X(idx==1,1),X(idx==1,2),'r.','MarkerSize',12)
hold on
plot(X(idx==2,1),X(idx==2,2),'b.','MarkerSize',12)
plot(C(:,1),C(:,2),'kx','MarkerSize',15,'LineWidth',3)  %标记聚类中心 
legend('Cluster 1','Cluster 2','Centroids','Location','NW')
title 'Cluster Assignments and Centroids'
hold off
kmeans

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap