Question about kmeans centroid
Show older comments
hi, i have a quick question about kmeans.
i randomly generated 1,000 number in the range of (0,1) and clustered them into 20.
however, i found the mean of each cluster is slightly different from their centroid. Why? By definition, they should be the same, right?
thanks.
1 Comment
Walter Roberson
on 20 Jul 2012
mean versus median ?
Accepted Answer
More Answers (2)
Peter Perkins
on 20 Jul 2012
Rebecca, are you seeing something like this?
>> x = rand(1000,1);
>> [idx,c] = kmeans(x,20);
>> c2 = grpstats(x,idx,@mean);
>> c - c2
ans =
0
0
-1.38777878078145e-17
0
0
0
0
0
0
-1.38777878078145e-17
0
0
0
-2.77555756156289e-17
0
0
-5.55111512312578e-17
0
0
0
That is to be expected, the differences are due to different rounding errors. Consider this:
>> x = rand(1000,1);
>> ( sum(x) - sum(x(randperm(length(x)))) ) / sum(x)
ans =
-7.87959181618481e-16
which is because the sums are in different order. Same idea.
If you're seeing something else, you;ll have to provide more info. Hope this helps.
rebecca
on 20 Jul 2012
0 votes
Categories
Find more on k-Means and k-Medoids Clustering in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!