K-means: see the initial centroids

Hi everyone,
Is it possible to see the randomly selected initial centroids when using the built-in kmeans function? I want to keep track of the centroids in each iteration.
Thanks.

 採用された回答

Massimo Zanetti
Massimo Zanetti 2016 年 12 月 2 日

1 投票

Yes it is possible.
kmeans allows you to set option parameters via the statset function. In the help page kmeans there some examples on how using stateset.
Set the parameter 'OutputFcn' as shown in this tutorial, for displaying iteration results in iterative solvers: https://it.mathworks.com/help/matlab/math/output-functions.html

4 件のコメント

Phu Lai
Phu Lai 2016 年 12 月 2 日
It seems that Output Function only works with optimization functions such as fminbnd, fminsearch or fzero. I tried implementing 'OutputFcn' like this but not working:
function [history] = kmeansIter(data,k)
history = [];
options = statset('OutputFcn', @myoutput,'MaxIter', 500, 'Display', 'final');
[clustIDX, clusters, interClustSum, Dist] = kmeans(data, k, 'options',options, ...
'distance','sqEuclidean', 'EmptyAction','singleton', 'replicates',3);
function stop = myoutput(x,optimvalues,state);
stop = false;
if isequal(state,'iter')
history = [history; x];
end
end
end
Can you please have a look to see if I've done anything wrong? Thanks very much.
Massimo Zanetti
Massimo Zanetti 2016 年 12 月 5 日
編集済み: Massimo Zanetti 2016 年 12 月 5 日
Ouch. Surprisingly, the stateset function has only some options available with kmeans. This is quite disappointing.
Therefore it seems there is no other way to circumvent your problem, than performing separate runs of kmeans (all with maxiter parameter set to 1) and save the results. Here is an example:
load fisheriris
X = meas(:,3:4);
MaxIt=4;
C = cell(1,MaxIt);
I = cell(1,MaxIt);
rng(1); % For reproducibility
[I{1},C{1}] = kmeans(X,3,'Display','iter','MaxIter',1);
for k=2:MaxIt
[I{k},C{k}] = kmeans(X,3,'Display','iter','Start',C{k-1},'MaxIter',1);
end
fig=figure;
for k=1:4
figure(fig);
gscatter(X(:,1),X(:,2),I{k}); grid on; hold on;
plot(C{k}(:,1),C{k}(:,2),'xk','MarkerSize',12,'LineWidth',5); hold off;
pause(.5);
end
Phu Lai
Phu Lai 2016 年 12 月 6 日
Thank you. I ended up following that approach. Also, do you know how to see the first initial centroids? (Let's say, C{0} in your example)
Massimo Zanetti
Massimo Zanetti 2016 年 12 月 6 日
Nope.. I have tried setting 'MaxIter'=0, but it seems it is the same as setting 'MaxIter'=1. The kmeans function is a black box :(

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by