Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

KMEANS help!!!!!!!!!!!

28 views
Skip to first unread message

Shweta

unread,
Mar 21, 2007, 10:48:05 AM3/21/07
to
Hey ,
I am using Matlab's KMEANS to cluster data. I find that when I call
the function each time I run the code it clusters differnently, as in
the same points are put in different clusters every time the function
is called. Do any of you happen to know why ?
Any inputs on this are highly appreciated...
Thanks
Shweta

Dan Pearce

unread,
Mar 21, 2007, 11:37:39 AM3/21/07
to
Can you show us an example from your code?

Tom Lane

unread,
Mar 21, 2007, 12:06:59 PM3/21/07
to
> I am using Matlab's KMEANS to cluster data. I find that when I call
> the function each time I run the code it clusters differnently, as in
> the same points are put in different clusters every time the function
> is called. Do any of you happen to know why ?

Shweta, the answer is here:

KMEANS K-means clustering.
...
[ ... ] = KMEANS(..., 'PARAM1',val1, 'PARAM2',val2, ...) allows you to
...
'Start' - Method used to choose initial cluster centroid positions,
sometimes known as "seeds". Choices are:
{'sample'} - Select K observations from X at random

The algorithm needs a starting value, which in this case is a set of K
univariate cluster means. I suspect if you fix the starting values, your
results won't vary.

There's another option:

'Replicates' - Number of times to repeat the clustering, each with a
new set of initial centroids [ positive integer | {1}]

This takes advantage of the random starting points by running the procedure
multiple times, and taking as a final answer the results that minimize the
within-cluster distances.

-- Tom


Peter Perkins

unread,
Mar 21, 2007, 2:43:27 PM3/21/07
to
Tom Lane wrote:

> The algorithm needs a starting value, which in this case is a set of K
> univariate cluster means. I suspect if you fix the starting values, your
> results won't vary.

It may also be that the different results on different runs are simply
permutations of the cluster numbers.

salih tuna

unread,
Sep 26, 2007, 4:53:14 PM9/26/07
to
hi,
i am also using kmeans clustering in matlab. i was
wondering if it is possible to manually choose the initial
points of the clusters? if yes how do i need to enter it?
thanks
salih

Shweta <shwetaan...@gmail.com> wrote in message
<ef511...@webcrossing.raydaftYaTP>...

Tom Lane

unread,
Sep 26, 2007, 5:31:18 PM9/26/07
to
> i am also using kmeans clustering in matlab. i was
> wondering if it is possible to manually choose the initial
> points of the clusters? if yes how do i need to enter it?

The answer is here:

>> help kmeans
KMEANS K-means clustering.
IDX = KMEANS(X, K) partitions the points in the N-by-P data matrix


...
'Start' - Method used to choose initial cluster centroid positions,
sometimes known as "seeds". Choices are:

...
matrix - A K-by-P matrix of starting locations. In this case,
you can pass in [] for K, and KMEANS infers K from
the first dimension of the matrix. You can also
supply a 3D array, implying a value for 'Replicates'
from the array's third dimension.

-- Tom


pires

unread,
Apr 13, 2008, 2:10:07 AM4/13/08
to
"Tom Lane" <tl...@mathworks.com> wrote in message
<fdej36$1hc$1...@fred.mathworks.com>...
Tom,
How to set???
No matter how i configure...Also failed. can u provide me
example...

Tom Lane

unread,
Apr 14, 2008, 11:09:37 AM4/14/08
to
"pires " <pire...@gmail.com> wrote in message
news:fts83v$b63$1...@fred.mathworks.com...

> No matter how i configure...Also failed. can u provide me
> example...

Here I use kmeans on the Fisher iris data, starting with the three species
means. I plot the results and compare the clusters to the known species.

-- Tom

>> load fisheriris
>> m = grpstats(meas,species)
m =
5.0060 3.4280 1.4620 0.2460
5.9360 2.7700 4.2600 1.3260
6.5880 2.9740 5.5520 2.0260
>> g = kmeans(meas,3,'start',m);
>> gplotmatrix(meas,[],g)
>> crosstab(g,species)
ans =
50 0 0
0 48 14
0 2 36


Kesede

unread,
Jan 22, 2010, 7:01:04 AM1/22/10
to
"Tom Lane" <tl...@mathworks.com> wrote in message <ftvs3h$lsp$1...@fred.mathworks.com>...

I executed his example and I obtained the same answer that I have been obtaining in my application, or be:

??? In an assignment A(I) = B, the number of elements in B and
I must be the same.

Error in ==> prwaitbar at 159
N(DEPTH) = n; % complete the admin

Error in ==> kmeans at 57
prwaitbar(maxit,'k-means clustering');

I already verified the code of the prwaitbar and I didn't get to solve the problem. Would know how to tell me how it could avoid this mistake?

Thank you very much!!

Pekka Kumpulainen

unread,
Jan 22, 2010, 7:52:21 AM1/22/10
to
"Kesede " <kesed...@gmail.com> wrote in message <hjc420$gq3$1...@fred.mathworks.com>...

Are you sure you are using the kmeans provided in the stats toolbox? Type
which kmeans

In my installations 2009a or 2010a the stats toolbox versions of kmeans don't have use anything called prwaitbar.

Kesede

unread,
Jan 22, 2010, 8:26:04 AM1/22/10
to
"Pekka Kumpulainen" <pekka.nospam...@tut.please.fi> wrote in message <hjc725$r17$1...@fred.mathworks.com>...

I typed,
>> which kmeans
C:\Matlab\toolbox\stats\kmeans.m

my version is 7.0.0.19920 (R14), what could be?

Tom Lane

unread,
Jan 22, 2010, 12:08:35 PM1/22/10
to
>> > I executed his example and I obtained the same answer that I have been
>> > obtaining in my application, or be:
>> >
>> > ??? In an assignment A(I) = B, the number of elements in B and
>> > I must be the same.
>> >
>> > Error in ==> prwaitbar at 159
>> > N(DEPTH) = n; % complete the admin
>> >
>> > Error in ==> kmeans at 57
>> > prwaitbar(maxit,'k-means clustering');
...

> I typed,
>>> which kmeans
> C:\Matlab\toolbox\stats\kmeans.m

There is a non-MathWorks thing called prtools. I don't know what it is, but
it has a kmeans function, a crossval function, and a prwaitbar function. It
appears you were calling the prtools function originally. But then later,
when you issued the "which" command, you didn't see that. I can't explain
the difference.

If you give the command

which kmeans -all

you may see both the Statistics Toolbox version and the prtools version. If
so, try temporarily removing prtools from your path. (Or just cd out of that
directory if that's where you are.) If you just see the statistics one
again, try repeating the example to see if it works this time.

-- Tom


Kesede

unread,
Jan 23, 2010, 7:44:03 AM1/23/10
to
"Tom Lane" <tl...@mathworks.com> wrote in message <hjcm2j$e8s$1...@fred.mathworks.com>...
Tom, thank you, thank you, thank you. Thank you very much!! Resolved problem.

yafit maymon

unread,
Oct 25, 2010, 10:34:04 AM10/25/10
to
"Tom Lane" <tl...@mathworks.com> wrote in message <ftvs3h$lsp$1...@fred.mathworks.com>...
hi u have also used the kmeans function with starting points matrix , as your exemple shows , but i have noticed that it wont allowe me to add more values for the function , for example :
[g ,sumd]= kmeans(meas,3,'start',m); - it aill return the centers in sumd but i would like it to return the sum of distance for each cluster ...
can you help me with that ??

thanks
yafit

Tom Lane

unread,
Oct 25, 2010, 11:26:17 AM10/25/10
to

> hi u have also used the kmeans function with starting points matrix , as
> your exemple shows , but i have noticed that it wont allowe me to add more
> values for the function , for example :
> [g ,sumd]= kmeans(meas,3,'start',m); - it aill return the centers in sumd
> but i would like it to return the sum of distance for each cluster ...
> can you help me with that ??

If you type "help kmeans" you will see:

[IDX, C, SUMD] = KMEANS(X, K)

You have named your second output variable sumd, so I suspect you really
want the third output. Here's one way to do that:

[g, ~, sumd] = kmeans(...)

-- Tom

yafit maymon

unread,
Oct 25, 2010, 1:45:06 PM10/25/10
to
"Tom Lane" <tl...@mathworks.nospam.com> wrote in message <ia47iq$h3i$1...@fred.mathworks.com>...
hi Tom

i though that was the problem , only didnt know how to solve it, your soulution seems simple i wish it worked, for some reason it doest , i keep getting error:
(x has data, m has 3 centers if i run it without the sign it work perfectly-can you see anything wrong???
>> [idx,~,sumd] = kmeans(x,3,'start',m);
??? [idx,~,sumd] = kmeans(x,3,'start',m);
|
Error: Expression or statement is incorrect--possibly unbalanced (, {, or [.

thank you!

Steven_Lord

unread,
Oct 25, 2010, 1:59:31 PM10/25/10
to

"yafit maymon" <yafi...@gmail.com> wrote in message
news:ia4fn2$30t$1...@fred.mathworks.com...

If you're using a version of MATLAB prior to release R2009b, this syntax
will not work for you.

http://www.mathworks.com/help/techdoc/rn/br5fo8o-1.html#br65zmd-1

Replace the tilde in the output argument with a dummy variable name.

--
Steve Lord
sl...@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

yafit maymon

unread,
Oct 25, 2010, 6:08:03 PM10/25/10
to
"Steven_Lord" <sl...@mathworks.com> wrote in message <ia4gi3$qsn$1...@fred.mathworks.com>...

hi steve,

so my 2008 version is the problem ..
i tried your solution and it works perfectly
thak you !

yafit

Raj Kodirekka

unread,
Feb 1, 2011, 3:46:03 AM2/1/11
to
Dear Tom,
i think you are very familiar with MATLAB kmeans, i am trying to rewrite this MATLAB kmeans for different distance measure.
as you know it has got 2 phases, Batch phase and online phase.
i know what is happening in Batch phase but i am not clear about online phase so can you please explain it in detail? it will be a great help to me.
thanks in advance.

"Tom Lane" <tl...@mathworks.nospam.com> wrote in message <ia47iq$h3i$1...@fred.mathworks.com>...
>
0 new messages