K-means Clustering Result Always Changes

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?

 採用された回答

Walter Roberson
Walter Roberson 2013 年 5 月 5 日

1 投票

%generate some initial cluster centers according to some deterministic algorithm
%in this case, I construct a space-diagonal equally spaced, but choose your
%own algorithm
minc = min(cobat, 1);
maxc = max(cobat, 1);
nsamp = size(cobat,1);
initialcenters = repmat(minc, nsamp, 1) + bsxfun(@times, (0:nsamp-1).', (maxc - minc) ./ (nsamp-1));
%Once you have constructed the initial centers, cluster using those centers
[cidx ctrs] = kmeans(cobat, k, 'dist', 'sqEuclidean', 'start', initialcenters);

6 件のコメント

Alvi Syahrin
Alvi Syahrin 2013 年 5 月 5 日
編集済み: Walter Roberson 2013 年 5 月 5 日
Thank you for the answer.
I still get confused of this line:
initialcenters = repmat(minc, nsamp, 1) + bsxfun(@times, (0:nsamp-1).', (maxc - minc) ./ (nsamp-1));
Do you mean; I have to construct how to initial the central of the cluster in matlab? But it (k-means) picks randomly, no method. Then do you have any idea?
Walter Roberson
Walter Roberson 2013 年 5 月 5 日
kmeans does not pick randomly if you pass the argument 'start' and a data matrix of initial cluster centroids.
That particular line constructs evenly-spaced points between the minimum and maximum values of each column -- similar to using linspace() but working on all the columns at once. The details are not really important: I just chose those centroids to have a bunch of centroids that were deterministically distributed around the entire space of values.
Alvi Syahrin
Alvi Syahrin 2013 年 5 月 5 日
Ok. It's been fixed now, by changing some scripts. Thank you for the idea.
Mohammad Al Nagdawi
Mohammad Al Nagdawi 2017 年 1 月 7 日
Excuse me! what are the parameters that kmean used to cluster image pixels? an intensity and position of the pixel or something else.
Thanks
esmat abdallah
esmat abdallah 2021 年 11 月 26 日
initialcenters = repmat(minc, nsamp, 1) + bsxfun(@times, (0:nsamp-1).', (maxc - minc) ./ (nsamp-1));
please, matlab out an error on this line : "Error using +
Matrix dimensions must agree."
what can i do ??
Walter Roberson
Walter Roberson 2021 年 11 月 26 日
%generate some initial cluster centers according to some deterministic algorithm
%in this case, I construct a space-diagonal equally spaced, but choose your
%own algorithm
minc = min(cobat, [], 1);
maxc = max(cobat, [], 1);
nsamp = size(cobat,1);
initialcenters = repmat(minc, nsamp, 1) + bsxfun(@times, (0:nsamp-1).', (maxc - minc) ./ (nsamp-1));
%Once you have constructed the initial centers, cluster using those centers
[cidx ctrs] = kmeans(cobat, k, 'dist', 'sqEuclidean', 'start', initialcenters);

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

その他の回答 (2 件)

the cyclist
the cyclist 2013 年 5 月 4 日

7 投票

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 件のコメント

Alvi Syahrin
Alvi Syahrin 2013 年 5 月 4 日
Thank you for the answer. I have MATLAB 7.11.0(R2010b), and when I tried that command, it's not working, getting an error for undefined function. Do you have any idea to solve this?
the cyclist
the cyclist 2013 年 5 月 4 日
Type
>> doc randstream
to see how to do it in your version.

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

Pallavi Saha
Pallavi Saha 2017 年 9 月 14 日

0 投票

I am facing the same issue inconsistency in the output of fcm. Can anyone help me

3 件のコメント

Walter Roberson
Walter Roberson 2017 年 9 月 14 日
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
Mehmet Volkan Ozdogan 2019 年 3 月 28 日
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
Walter Roberson 2019 年 3 月 29 日
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.

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

Community Treasure Hunt

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

Start Hunting!

Translated by