different output in kmeans

i used kmeans for clustering similar images.... if i run the code first i get the correct clusters..... but without closing matlab if i execute the second time for the same image, it is clustering different output.... why like that..... what shud i do to get the same output whenever i execute the code... please do reply.....

 Respuesta aceptada

Youssef  Khmou
Youssef Khmou el 6 de Mayo de 2013

1 voto

hi, i think this question has been asked before, the reason is that the K-means algorithm starts with random partition so every time you run the code, you get the same result but with different RMSE.
(try to clear the Workspace and re-run ...)

5 comentarios

Elysi Cochin
Elysi Cochin el 6 de Mayo de 2013
yes sir i too guessed it may be of the random partitioning of kmeans.... so i cleared all the variables using "clear all" and executed it..... but the same thing.... unless i close matlab and re-run it i'm not getting the same output... sir is there anything i can do to get same output..... please do reply sir....
Youssef  Khmou
Youssef Khmou el 6 de Mayo de 2013
José-Luis
José-Luis el 6 de Mayo de 2013
Editada: José-Luis el 6 de Mayo de 2013
@Youssef: no, you don't get the same result, if by result you mean centers. It might happen that you get the same RMSE for different centers. That's rather unlikely though. Or the same centers and different RMSE, but that's also very unlikely.
Walter Roberson
Walter Roberson el 7 de Mayo de 2013
Could you indicate
size(repmat(minc, nsamp, 1))
size( bsxfun(@times, (0:nsamp-1).', (maxc - minc) ./
(nsamp-1)) )

Iniciar sesión para comentar.

Más respuestas (1)

José-Luis
José-Luis el 6 de Mayo de 2013
Editada: José-Luis el 6 de Mayo de 2013

1 voto

An option is to reset the random number generator to its initial state every time before running your code:
rng default % ->This is the important bit
X = [randn(100,2)+ones(100,2);...
randn(100,2)-ones(100,2)];
opts = statset('Display','final');
[idx,ctrs] = kmeans(X,2,...
'Distance','city',...
'Replicates',5,...
'Options',opts);
This will always produce the same result, but it sorts of beat the purpose of the function and might produce bad results.

2 comentarios

José-Luis
José-Luis el 6 de Mayo de 2013
Editada: José-Luis el 6 de Mayo de 2013
For example:
X = [randn(100,2)+ones(100,2);...
randn(100,2)-ones(100,2)];
opts = statset('Display','final');
[idx,ctrs] = kmeans(X,2,...
'Distance','city',...
'Replicates',1,...
'Options',opts,...
'start',[0.25 0.25; 0.75 0.75]);
But that does not guarantee that the result will always be the same.
Elysi Cochin
Elysi Cochin el 7 de Mayo de 2013
thank you all for your valuable suggestions in helping me to solve my problem..... thank you all once again......

Iniciar sesión para comentar.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by