資料採礦之clara演算法原理及執行個體(代碼中有bug)

來源:互聯網
上載者:User

標籤:clara   大資料聚類   聚類   k-mediod   

繼上兩篇文章介紹聚類中基於劃分思想的k-means演算法和k-mediod演算法

本文將繼續介紹另外一種基於劃分思想的k-mediod演算法-----clara演算法





clara演算法可以說是對k-mediod演算法的一種改進,就如同k-mediod演算法對

k-means演算法的改進一樣.clara(clustering large application)演算法是應用

於大規模資料的聚類.而其核心演算法還是利用k-mediod演算法.只是這種演算法

彌補了k-mediod演算法只能應用於小規模資料的缺陷.






clara演算法的核心是,先對大規模資料進行多次採樣,每次採樣樣本進行med-diod

聚類,然後將多次採樣的樣本聚類中心進行比較,選出最優的聚類中心.

當然clara演算法也有一定的缺陷,因為它依賴於抽樣次數,每次樣本資料

是否均勻分布,以及抽樣樣本的大小.儘管這樣,clara演算法還是為我們

提供了一種進行大規模資料聚類的方法.






clara演算法的具體描述如下:

1.對大規模資料進行多次採樣得到採樣樣本


2.對每次採樣的樣本進行k-mediod聚類,得到多組聚類中心


3.求出每組聚類中心到其他所有點距離和.


4.找出這幾組距離和的最小值.距離和最小的那組就是最優的聚類中心.


5.然後將大規模資料按照距離聚類到這組最優聚類中心



matlab模擬代碼如下:

clc;clear;load Data3.mat;k=3; %給定的類別數目time=5;%time為抽樣的次數number=30;%number為抽樣本個數for T=1:time    ClomStaticSample=zeros(1,number);    ClomStaticSample=randsample(ClomStatic,number);   %ClomStaticSample就是樣本資料                                                      %接下來對樣本資料使用kmediod演算法進行聚類                                                          %產生三個隨機整數,隨機聚類中心    p=randperm(number);    Temp=p(1:k);    Center=zeros(1,k);    for j=1:k        Center(j)=ClomStaticSample(Temp(j));    end    [ClomStaticSample]=sort(ClomStaticSample);        TempDistance=zeros(number,3);           %暫存差值         while 1        Circulm=1;                          %迴圈控制        p1=1;        p2=1;        p3=1;        if(Circulm~=1)            clear Group1 Group2 Group3;           end        for i=1:number            for j=1:3                TempDistance(i,j)=abs(ClomStaticSample(i)-Center(j));            end            [RowMin RowIndex]=min(TempDistance(i,:));            if(RowIndex(1)==1)                Group1(p1)=ClomStaticSample(i);                p1=p1+1;            elseif(RowIndex(1)==2)                Group2(p2)=ClomStaticSample(i);                p2=p2+1;            elseif(RowIndex(1)==3)                Group3(p3)=ClomStaticSample(i);                p3=p3+1;            end        end            len1=length(Group1);            len2=length(Group2);            len3=length(Group3);                              %分別計算每個類中除開類中心的點到其他所有點的距離和E,E最小時為該類新的聚類中心.                  E=zeros(1,len1-1);                  q1=1;                  for j=1:len1                      for i=1:number                        if(Group1(j)~=Center(1)&&i~=j)                            E(q1)=floor(abs(Group1(j)-ClomStaticSample(i)));                            q1=q1+1;                        end                      end                  end                  NewCenter(1)=min(E);                 E=zeros(1,len2-1);                  q2=1;                  for j=1:len2                      for i=1:number                        if(Group2(j)~=Center(2)&&i~=j)                            E(q2)=floor(abs(Group2(j)-ClomStaticSample(i)));                            q2=q2+1;                        end                      end                  end                  NewCenter(2)=min(E);                  E=zeros(1,len3-1);                  q3=1;                  for j=1:len3                      for i=1:number                        if(Group3(j)~=Center(3)&&i~=j)                            E(q3)=floor(abs(Group3(j)-ClomStaticSample(i)));                            q3=q3+1;                        end                      end                  end                  NewCenter(3)=min(E);            %判斷新的類和舊類的聚類中心是否不同,不同則繼續聚類,否則聚類結束            JudgeEqual=zeros(1,k);            for i=1:k                JudgeEqual=(NewCenter==Center);            end            S=0;            for i=1:k                if(JudgeEqual(i)==1)                    S=S+1;                end            end            if(S==3)                break;            end            Circulm=Circulm+1;     end     CenterSum5=zeros(time,k);           %儲存每次抽樣後kmediod聚類中心的結果值.     CenterSum5(i,1)=Center(1);     CenterSum5(i,2)=Center(2);     CenterSum5(i,3)=Center(3);end%計算每次聚類中心點到其他所有點的距離和的最小值即為最優聚類中心Sum=zeros(1,time);for i=1:time    for j=1:k        for r=1:number-1            if( CenterSum5(i,j)~=ClomStaticSample(r))            Sum(i)=Sum(i)+CenterSum5(i,j)-ClomStaticSample(r);            end        end    endend[SumOrder CenterEnd]=sort(Sum);%最優聚類中心即為Center(CenterEnd);%對大資料進行最終的聚類(按照選擇出來的最優聚類中心)        q1=1;        q2=1;        q3=1;        for i=1:length(ClomStatic)            for j=1:3                EndTempDistance(i,j)=abs(ClomStatic(i)-CenterSum5(CenterEnd,j));            end            [RowMin RowIndex]=min(EndTempDistance(i,:));            if(RowIndex(1)==1)                EndGroup1(q1)=ClomStatic(i);                q1=q1+1;            elseif(RowIndex(1)==2)                EndGroup2(q2)=ClomStatic(i);                q2=q2+1;            elseif(RowIndex(1)==3)                EndGroup3(q3)=ClomStatic(i);                q3=q3+1;            end        end




轉載請註明文章小劉

資料採礦之clara演算法原理及執行個體(代碼中有bug)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.