付費(fèi)下載
下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
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è)簡單的 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. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 公共衛(wèi)生管理獎(jiǎng)罰制度
- 關(guān)于職業(yè)安全衛(wèi)生制度
- 游泳場衛(wèi)生制度
- 安全衛(wèi)生制度
- 手術(shù)室衛(wèi)生制度
- 企業(yè)車間衛(wèi)生標(biāo)準(zhǔn)制度
- 地水廠衛(wèi)生制度
- 中央廚房衛(wèi)生標(biāo)準(zhǔn)制度
- 茶吧衛(wèi)生制度
- 鄉(xiāng)衛(wèi)生院醫(yī)學(xué)證明制度
- 2026中好建造(安徽)科技有限公司招聘45人筆試備考試題及答案解析
- 2025年輔警面試考試復(fù)習(xí)題庫目及解析答案
- 北師大版三年級數(shù)學(xué)(上)期末家長會(huì)-三載深耕學(xué)有所成【課件】
- 風(fēng)機(jī)安全鏈課件
- 2025年企業(yè)設(shè)備故障處理手冊
- 紀(jì)檢部部長競選課件
- 遼寧省沈陽市沈河區(qū)2025-2026學(xué)年度上學(xué)期九年級期末語文試卷(含答案)
- DB36∕T 2141-2025 兒童福利機(jī)構(gòu)兒童檔案管理規(guī)范
- 玻璃幕墻施工專項(xiàng)方案
- GB/T 21790-2025閃點(diǎn)的測定用小型閉杯試驗(yàn)儀測定閃燃非閃燃和閃點(diǎn)的方法
- 肝臟代謝重編程-洞察與解讀
評論
0/150
提交評論