KMeans和KMedoid 的Matlab实现

K Means和KMedoid算法是聚类算法中比较普遍的方法,本文讲了其原理和matlab中实现的代码。



1.目标:

       找出一个分割,使得距离平方和最小


2.K-Means算法:

       1. 将数据分为k个非空子集

       2. 计算每个类中心点(k-means中用所有点的平均值,K-medoid用离该平均值最近的一个点)center

       3. 将每个object聚类到最近的center

       4. 返回2,当聚类结果不再变化的时候stop


   复杂度:

       O(kndt)

       -计算两点间距离:d

       -指定类:O(kn)   ,k是类数

       -迭代次数上限:t


3.K-Medoids算法:

       1. 随机选择k个点作为初始medoid

       2.将每个object聚类到最近的medoid

       3. 更新每个类的medoid,计算objective function 

       4. 选择最佳参数

       4. 返回2,当各类medoid不再变化的时候stop


    复杂度:

       O((n^2)d)

       -计算各点间两两距离O((n^2)d)

       -指定类:O(kn)   ,k是类数


4.特点:

       -聚类结果与初始点有关(因为是做steepest descent from a random initial starting oint)

       -是局部最优

       -在实际做的时候,随机选择多组初始点,最后选择拥有最低TSD(Totoal Squared Distance)的那组




Kmeans KMedoid Implementation with matlab:

===================

下面是我用matlab上的实现:

说明:fea为训练样本数据,gnd为样本标号。算法中的思想和上面写的一模一样,在最后的判断accuracy方面,由于聚类和分类不同,只是得到一些 cluster ,而并不知道这些 cluster 应该被打上什么标签,或者说。由于我们的目的是衡量聚类算法的 performance ,因此直接假定这一步能实现最优的对应关系,将每个 cluster 对应到一类上去。一种办法是枚举所有可能的情况并选出最优解,另外,对于这样的问题,我们还可以用 Hungarian algorithm 来求解。具体的Hungarian代码我放在了资源里,调用方法已经写在下面函数中了。下面给出Kmeans&Kmedoid主函数。


Kmeans.m 函数:

[cpp]  view plain copy
  1. function [ accuracy,MIhat ] = KMeans( K,mode )  
  2.   
  3. % Artificial Intelligence & Data Mining - KMeans & K-Medoids Clustering  
  4. % Author: Sophia_qing @ ZJU  
  5. % CreateTime: 2012-11-18  
  6. % Function: Clustering  
  7. %  -K: number of clusters  
  8. %  -mode:   
  9. %   1: use kmeans cluster algorithm in matlab  
  10. %   2: k_medroid algorithm: use data points as k centers  
  11. %   3: k_means algorithm: use average as k centers  
  12.   
  13. global N_features;  
  14. global N_samples;  
  15. global fea;  
  16. global gnd;  
  17.   
  18. switch (mode)  
  19.     case 1 %call system function KMeans  
  20.         label = kmeans(fea,K);  
  21.         [label,accuracy] = cal_accuracy(gnd,label);  
  22.           
  23.     case 2%use kmedroid method  
  24.         for testcase = 1:10% do 10 times to get rid of the influence from Initial_center  
  25.             K_center = Initial_center(fea,K); %select initial points randomly  
  26.             changed_label = N_samples;  
  27.             label = zeros(1,N_samples);  
  28.             iteration_times = 0;  
  29.             while changed_label~=0  
  30.                 cls_label = cell(1,K);  
  31.                 for i = 1: N_samples  
  32.                     for j = 1 : K  
  33.                         D(j) = dis(fea(i,:),K_center(j,:));  
  34.                     end  
  35.                     [~,label(i)] = min(D);  
  36.                     cls_label{label(i)} = [cls_label{label(i)} i];  
  37.                 end  
  38.                 changed_label = 0;  
  39.                 cls_center = zeros(K,N_features);  
  40.                 for i = 1 : K  
  41.                     cls_center(i,:) = mean(fea(cls_label{i},:));  
  42.                     D1 = [];  
  43.                     for j = 1:size(cls_label{i},2)%number of samples clsutered in i-th class  
  44.                         D1(j) = dis(cls_center(i,:),fea(cls_label{i}(j),:));  
  45.                     end  
  46.                     [~,min_ind] = min(D1);  
  47.                     if ~isequal(K_center(i,:),fea(cls_label{i}(min_ind),:))  
  48.                         K_center(i,:) = fea(cls_label{i}(min_ind),:);  
  49.                         changed_label = changed_label+1;  
  50.                     end  
  51.                 end  
  52.                 iteration_times = iteration_times+1;  
  53.             end  
  54.             [label,acc(testcase)] = cal_accuracy(gnd,label);  
  55.         end  
  56.         accuracy = max(acc);  
  57.           
  58.     case 3%use k-means method  
  59.         for testcase = 1:10% do 10 times to get rid of the influence from Initial_center  
  60.             K_center = Initial_center(fea,K); %select initial points randomly  
  61.             changed_label = N_samples;  
  62.             label = zeros(1,N_samples);  
  63.             label_new = zeros(1,N_samples);  
  64.             while changed_label~=0  
  65.                 cls_label = cell(1,K);  
  66.                 changed_label = 0;  
  67.                 for i = 1: N_samples  
  68.                     for j = 1 : K  
  69.                         D(j) = dis(fea(i,:),K_center(j,:));  
  70.                     end  
  71.                     [~,label_new(i)] = min(D);  
  72.                     if(label_new(i)~=label(i))  
  73.                         changed_label = changed_label+1;  
  74.                     end;  
  75.                     cls_label{label_new(i)} = [cls_label{label_new(i)} i];  
  76.                 end  
  77.                 label = label_new;  
  78.                   
  79.                 for i = 1 : K  %recalculate k centroid  
  80.                     K_center(i,:) = mean(fea(cls_label{i},:));  
  81.                 end  
  82.             end  
  83.              [label,acc(testcase)] = cal_accuracy(gnd,label);  
  84.         end  
  85.         accuracy = max(acc);  
  86. end  
  87.   
  88. MIhat = MutualInfo(gnd,label);  
  89.   
  90.   
  91.     function center = Initial_center(X,K)  
  92.         rnd_Idx = randperm(N_samples,K);  
  93.         center = X(rnd_Idx,:);  
  94.     end  
  95.   
  96.     function res = dis(X1,X2)  
  97.         res = norm(X1-X2);  
  98.     end  
  99.   
  100.     function [res,acc] = cal_accuracy(gnd,estimate_label)  
  101.         res = bestMap(gnd,estimate_label);  
  102.         acc = length(find(gnd == res))/length(gnd);  
  103.     end  
  104. end  


实验结果分析:

对上面得到的accuracy进行画图,横坐标为10个数据集,纵坐标为在其上进行聚类的准确率。

其中,auto为matlab内部kmeans函数。

画图:

[cpp]  view plain copy
  1. function [  ] = Plot( A,B,C )  
  2. %PLOT Summary of this function goes here  
  3. %   Detailed explanation goes here  
  4. figure;  
  5. k = 1:10;  
  6. plot(k,A,'-r',k,B,'-b',k,C,'-g');  
  7. legend('auto','medoid','means');  
  8.   
  9.   
  10. end  

结果:


5类聚类:



7类聚类:





关于Machine Learning更多的学习资料与相关讨论将继续更新,敬请关注本博客和新浪微博Sophia_qing


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值