我刚刚开始学习编码,并开始编写标准的k-means算法。我尝试在由三个不同的高斯生成的数据集上实现,它似乎运行良好。但是我在虹膜数据集上尝试了它,并且偶尔(大约三分之一)我的函数只返回两个集合,换句话说它只返回两个集群。
我看了一下本地MATLAB kmeans函数的代码,但由于缺乏编码知识,我最终迷路了。我真的很感激任何帮助!
function [R,C,P,it] = mykmeans(X,K)
% X -- data matrix
% K -- number of clusters
% C -- partition sets
% P -- matrix of prototypes
% R -- binary indicator matrix: R(i,j) specifies whether the ith data is
% classified into jth cluster
% it -- number of iterations until convergence
% N points with M dimensions
[N,M] = size(X) ;
%% Initialisation
% At this step we randomly partition the data matrix into K equally sized
% matrices and compute the centre of each of these matrices.
% I -- randomised index vector
% v -- number of data points assigned to each cluster
% U -- randomly partitioned matrices
v = N/K ;
C = cell(K,1) ;
U = cell(K,1) ;
I = randperm(N) ;
oldR = zeros(N,K) ;
% C{1} = X(I(1:v),:) ;
% U{1} = mean(X(I(1:v),:)) ;
for k=1:K
C{k} = X(I(1+v*(k-1):k*v),:) ;
U{k} = mean(C{k}) ;
end
P = cell2mat(U) ;
converged = 0 ;
it = 0 ;
while converged ~= 1
%% Assignment step
% Each element of D{n} contains squared euclidean distance of nth data
% point from the kth prototype
D = cell(N,1) ;
R = zeros(N,K) ;
for n=1:N
D{n} = sum((repmat(X(n,:),K,1) - P).^2,2) ;
[~,k] = min(D{n}) ;
R(n,k) = 1 ;
end
%% Update step
C = cell(K,1) ; % reset C
for k=1:K
for n=1:N
P(k,:) = R(n,k)*X(n,:) + P(k,:) ; % compute numerator of mean vector
if R(n,k) == 1
C{k} = [C{k};X(n,:)] ;
end
end
end
P = P ./ (sum(R)') ; % divide by denominator of mean vectors to get prototypes
%% Check for convergence
if sum(sum(R == oldR))==N*K || it == 100 % convergence criteria
converged = 1 ;
else
oldR = R ;
it = it+1 ;
end
end %while
答案 0 :(得分:3)
问题确实似乎不是编码问题,而是理解k-means的问题。
事实上,在k-means期间,群集可能变空。您需要在代码中对此进行说明,否则结果中的聚类数可能小于k。
可能的解决方案可能是:
因此,一般方法如下:
可以找到空集群问题的一个很好的例子here。