MATLAB Answers

K-means: see the initial centroids

3 views (last 30 days)
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.

  0 Comments

Sign in to comment.

Accepted Answer

Massimo Zanetti
Massimo Zanetti on 2 Dec 2016
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 Comments

Show 1 older comment
Massimo Zanetti
Massimo Zanetti on 5 Dec 2016
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 on 6 Dec 2016
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 on 6 Dec 2016
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 :(

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!