kmeans演算法matlab
① 怎樣用matlab實現多維K-means聚類演算法
直接用kmeans函數。。。
idx = kmeans(X,k)
idx = kmeans(X,k,Name,Value)
[idx,C] = kmeans(___)
[idx,C,sumd] = kmeans(___)
[idx,C,sumd,D] = kmeans(___)
idx = kmeans(X,k) performs k-means clustering to partition the observations of the n-by-p data matrix X into k clusters, and returns an n-by-1 vector (idx) containing cluster indices of each observation. Rows of X correspond to points and columns correspond to variables.
By default, kmeans uses the squared Euclidean distance measure and the k-means++ algorithm for cluster center initialization.
example
idx = kmeans(X,k,Name,Value) returns the cluster indices with additional options specified by one or more Name,Value pair arguments.
For example, specify the cosine distance, the number of times to repeat the clustering using new initial values, or to use parallel computing.
example
[idx,C] = kmeans(___) returns the k cluster centroid locations in the k-by-p matrix C.
example
[idx,C,sumd] = kmeans(___) returns the within-cluster sums of point-to-centroid distances in the k-by-1 vector sumd.
example
[idx,C,sumd,D] = kmeans(___) returns distances from each point to every centroid in the n-by-k matrix D.
② 如何編寫求K-均值聚類演算法的Matlab程序
在聚類分析中,K-均值聚類演算法(k-means algorithm)是無監督分類中的一種基本方法,其也稱為C-均值演算法,其基本思想是:通過迭代的方法,逐次更新各聚類中心的值,直至得到最好的聚類結果。 假設要把樣本集分為c個類別,演算法如下: (1)適當選擇c個類的初始中心; (2)在第k次迭代中,對任意一個樣本,求其到c個中心的距離,將該樣本歸到距離最短的中心所在的類, (3)利用均值等方法更新該類的中心值; (4)對於所有的c個聚類中心,如果利用(2)(3)的迭代法更新後,值保持不變,則迭代結束,否則繼續迭代。 下面介紹作者編寫的一個分兩類的程序,可以把其作為函數調用。 %% function [samp1,samp2]=kmeans(samp); 作為調用函數時去掉注釋符 samp=[11.1506 6.7222 2.3139 5.9018 11.0827 5.7459 13.2174 13.8243 4.8005 0.9370 12.3576]; %樣本集 [l0 l]=size(samp); %%利用均值把樣本分為兩類,再將每類的均值作為聚類中心 th0=mean(samp);n1=0;n2=0;c1=0.0;c1=double(c1);c2=c1;for i=1:lif samp(i)<th0 c1=c1+samp(i);n1=n1+1;elsec2=c2+samp(i);n2=n2+1;endendc1=c1/n1;c2=c2/n2; %初始聚類中心t=0;cl1=c1;cl2=c2; c11=c1;c22=c2; %聚類中心while t==0samp1=zeros(1,l); samp2=samp1;n1=1;n2=1;for i=1:lif abs(samp(i)-c11)<abs(samp(i)-c22) samp1(n1)=samp(i); cl1=cl1+samp(i);n1=n1+1; c11=cl1/n1;elsesamp2(n2)=samp(i); cl2=cl2+samp(i);n2=n2+1; c22=cl2/n2;endendif c11==c1 && c22==c2t=1;endcl1=c11;cl2=c22; c1=c11;c2=c22; end %samp1,samp2為聚類的結果。 初始中心值這里採用均值的辦法,也可以根據問題的性質,用經驗的方法來確定,或者將樣本集隨機分成c類,計算每類的均值。 k-均值演算法需要事先知道分類的數量,這是其不足之處。
③ 怎樣用matlab實現多維K-means聚類演算法
function [ labels ] = kmeans_clustering( data, k )
[num,~]=size(data);
ind = randperm(num);
ind = ind(1:k);
centers = data(ind,:);
d=inf;
labels = nan(num,1);
while d>0
labels0 = labels;
dist = pdist2(data, centers);
[~,labels] = min(dist,[],2);
d= sum(labels0 ~= labels);
for i=1:k
centers(i,:)=mean(data(labels == i,:),1);
end
end
end