zoukankan      html  css  js  c++  java
  • [matlab] 23.matlab自带kmeans函数 实现聚类

    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

  • 相关阅读:
    Codeforces 1072
    XDOJ 1046
    BZOJ 1002
    BZOJ 1001
    BZOJ 1500/Luogu 2042
    nowcoder 211B
    BZOJ 3224
    BZOJ 1150
    POJ 2442
    二叉堆和二叉搜索树进阶
  • 原文地址:https://www.cnblogs.com/clemente/p/9646364.html
Copyright © 2011-2022 走看看