K-means Clustering Result Always Changes
Mostrar comentarios más antiguos
I'm working on k-means in MATLAB. Here are my codes:
load cobat.txt
k=input('Enter the number of cluster: ');
if k<8
[cidx ctrs]=kmeans(cobat, k, 'dist', 'sqEuclidean');
Z = [cobat cidx]
else
h=msgbox('Must be less than eight');
end
"cobat" is the file of mine and here it looks:
65 80 55
45 75 78
36 67 66
65 78 88
79 80 72
77 85 65
76 77 79
65 67 88
85 76 88
56 76 65
My problem is everytime I run the code, it always shows different result, different cluster. How can I keep the clustering result always the same?
Respuesta aceptada
Más respuestas (2)
the cyclist
el 4 de Mayo de 2013
K-means clustering uses randomness as part of the algorithm Try setting the seed of the random number generator before you start. If you have a relatively new version of MATLAB, you can do this with the rng() command. Put
rng(1)
at the beginning of your code.
2 comentarios
Alvi Syahrin
el 4 de Mayo de 2013
the cyclist
el 4 de Mayo de 2013
Type
>> doc randstream
to see how to do it in your version.
Pallavi Saha
el 14 de Sept. de 2017
0 votos
I am facing the same issue inconsistency in the output of fcm. Can anyone help me
3 comentarios
Walter Roberson
el 14 de Sept. de 2017
K-means select cluster positions randomly unless you pass it specific start positions.
To stop the output from being random, you can either pass specific start positions for the clusters, or you can use rng() to set the random seed to a constant so that the same sequence of random values will be created each time.
Mehmet Volkan Ozdogan
el 28 de Mzo. de 2019
Hi,
I have a question about rng(). If we use rng() command, K-means algortihm stil repeats until the results are getting convergenced to the best. Is that right?
Thank you
Walter Roberson
el 29 de Mzo. de 2019
Yes.
rng(SomeParticularNumericSeed)
just ensures that it will always use the same random number sequence provided that no other random numbers are asked for between the rng() call and the kmeans call.
Categorías
Más información sobre k-Means and k-Medoids Clustering en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!