總結(jié)各種代碼聚類分析k-means matlab_第1頁(yè)
總結(jié)各種代碼聚類分析k-means matlab_第2頁(yè)
總結(jié)各種代碼聚類分析k-means matlab_第3頁(yè)
總結(jié)各種代碼聚類分析k-means matlab_第4頁(yè)
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、function y=kMeansCluster(m,k,isRand)%kMeansCluster - Simple k means clustering algorithmAuthor: Kardi Teknomo, Ph.D.Pure: classify the objects in data matrix based on the attributesCriteria: minimize Euclidean distance betn centroids and object posFor more explanation of the algorithm, see HYPERLINK

2、 http:/p/ http:/p/kardi/tutorial/kMean/index.html%Output: matrix data plus an additional column represent the group of each objectExle:mm=112451; 2 1;1;1;3;443;54or in a nice formk=2kMeansCluster(m,k)produm=124511341;1;2;2Input: mkrequired, matrixoptional, numberdata:objects in rows and attributes i

3、n columnsof groups (default = 1)isRand - optional, if using random initialization isRand=1, otherwise input anynumber (default)%it will assign thek data as initial centroidsLocal fc giVariables-row number of dacentroid coordinatehat belongsize (1:k,to group i 1:maxCol)size (1:maxRow)current iteratio

4、n group matrixscalarscalar scalariterator number of rowsnumber of columnsmaxCol maxRow tempzhe data matrix m = number of attributes he data matrix m = number of objectsmatrix size (1:maxRow)previous iteration groupminimum value (not needed)%if nargin3,if nargin2,isRand=0;k=1;endendmaxRow, maxCol=siz

5、e(m)if maxRow=k,y=m, 1:maxRow else% initial value of centroidif isRand,p =forrandperm(size(m,1); i=1:kc(i,:)=m(p(i),:)% random initializationend elsefori=1:kc(i,:)=m(i,:)% sequential initializationendendtemp=zeros(maxRow,1);%initialize as zero vectorwhile 1,d=DistMatrix(m,c); z,g=min(d,2); if g=temp

6、,break; elsetemp=g;endfor i=1:kf=find(g=i); if f%calculate objcets-centroid distanfindgroup matrix g%stopthe iteration%copygroup matrix to temporary variable%onlycompute centroid if f is not emptyc(i,:)=mean(m(find(g=i),:),1);endendendy=m,g;endThefunction kMeansCluster above call function DistMatrix

7、 as shownhecode below. It works for multi-dimenal Euclidean distance. Learn about othertype of distance here.function d=DistMatrix(A,B)% DISTMATRIX return distance matrix betn po y2 . w2% Copyright (c) 2005 by Kardi Teknomo,/kardi/%s in A=x1 y1 . w1and in B=x2 HYPERLINK http:/p/ http:/p% Numbers of

8、rows (represent pos) in A and B are not nesarilythe same.% It can be use for distance-in-a-slice (Spacing) or n-slice (Headway),%distance-bet% A and B must contas),he samber of columns (represent variablesof n dimen%column is the X coordinates, second column is the Y coordinates,andsoon.%The andexdi

9、stance matrix is distance bet pos in B as columns.le: Spacing= dist(A,A)n pos in A as rowsHeadway= dist(A,B), with hA = hBor hA=hBA=1 2 3; 4 5 6; 24 6; 15.83;7.00;7.48;5.8323; B=45 1; 6 2 0dist(A,B)=4.695.005.484.69dist(B,A)=4.695.835.007.005.487.484.69;5.83%hA,wA=size(A);hB,wB=size(B);if wA = wB,er

10、ror( second dimenof A and Bmust be the same);endfor k=1:wACk= repmat(A(:,k),1,hB);Dk= repmat(B(:,k),1,hA);end S=zeros(hA,hB); for k=1:wAS=S+(Ck-Dk).2;end d=sqrt(S);%這是一個(gè)簡(jiǎn)單的 k 均值聚類批處理函數(shù)%待分類的樣本 x=mvnrnd(mu,siguma,20)%idx3=kmeans(x,3,distance,city);或者%idx4=kmeans(x,4,dist,city,display,iter);這個(gè)可以顯示出每次迭代

11、的距離和%顯示分類輪廓圖silh4,h=silhouette(x,idx4,city);xlable(silhouette%value);ylable(cluster)%mean(silh5) 結(jié)果越接近 1 越好mu1=1,1; sigma1=0.5 0;0 0.5;x1=mvnrnd(mu1,sigma1,10); mu2=7,7;sigma2=0.5 0;0 0.5;x2=mvnrnd(mu2,sigma2,10); x=x1;x2 plot(x(:,1),x(:,2),bo);idx2,c=kmeans(x,2,dist,city,display,iter); figure(2);silh2,h=

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論