6
$\begingroup$

I use the ClusterComponent function to get the k-means clustering:

Needs["HierarchicalClustering`"]
cl = ClusteringComponents[data, 4, 1, 
   DistanceFunction -> CorrelationDistance, Method -> "KMeans"];

It returns a vector of cluster labels, say: $\{1,1,2,3,2,2,2,4,4,\ldots\}$.

How can I get the cluster centroids from this data?

$\endgroup$
3
$\begingroup$

The centroid for a finite point set is defined as

$$C=\frac{x_1+x_2+...+x_n}{n}$$

To calculate the centroid from the cluster table just get the position of all points of a single cluster, sum them up and divide by the number of points.

You haven't provided example data so I made a little example myself.

i = Import["http://jiamom.files.wordpress.com/2012/03/yin-yang.png"];
i = ImageResize[i, 200];

cl = ClusteringComponents[i, DistanceFunction -> CorrelationDistance, 
   Method -> "KMeans"];
cntr = Mean[Position[cl, #]] & /@ Range[2];

Show[ImageRotate@i, 
 ListPlot[Transpose@{cntr}, 
  PlotLegends -> {"WhiteCentroid", "BlackCentroid"}]]

enter image description here

$\endgroup$
  • $\begingroup$ I believe the author of the thread meant the actual centers of the clustering process. Those do not correspond to the mean value of the position vectors. Especially not with considering that the DistanceFunction for the KMeans clustering process has been set to CorrelationDistance. $\endgroup$ – Wizard Sep 24 '14 at 21:09
2
$\begingroup$

Update:

xd = RandomReal[10, {100, 63}];
clX = ClusteringComponents[xd, 4, 1, 
   DistanceFunction -> CorrelationDistance, Method -> "KMeans"];
centroids = ComponentMeasurements[{clX}, "Centroid"]
(* {1 -> {50.6667,0.5},2 -> {53.9583,0.5},3 -> {50.6739,0.5}, 4 -> {44.3261,0.5}} *)

clxd = Transpose[ConstantArray[clX, {63}]];
Show[ArrayPlot[xd, ImageSize -> 400, AspectRatio -> 1],
 ArrayPlot[clxd, ImageSize -> 400, AspectRatio -> 1,
  ColorRules -> {1 -> Directive[Opacity[.5], Red],
    2 -> Directive[Opacity[.5], Green],
    3 -> Directive[Opacity[.5], Blue],
    4 -> Directive[Opacity[.5], Yellow]}]]

enter image description here


Using @paw's set-up, you can also use the "Centroid" property of ComponentMeasurements as follows:

i = Import["http://jiamom.files.wordpress.com/2012/03/yin-yang.png"];
i = ImageResize[i, 200];
cl = ClusteringComponents[i, DistanceFunction -> CorrelationDistance, Method -> "KMeans"];

cm = ComponentMeasurements[{cl, i}, "Centroid"]
(* {1 -> {79.7603,112.6136},2 -> {126.6629,83.3832}} *)

Show[i, Epilog -> {PointSize[Large], Blue, Point[First/@Last/@cm], Yellow, Point[Last/@Last/@cm]}]

enter image description here

$\endgroup$
  • $\begingroup$ My data looks like x=RandomReal[10,{100,63}]. Each point to cluster has 63 dimensions... When using ComponentMeasurements[{cl,x},"Centroid"], I get an error message because it expects an image... $\endgroup$ – user13675 Sep 27 '14 at 17:20
  • $\begingroup$ @user13675, i suggest you edit your question to add this information about the data. $\endgroup$ – kglr Sep 27 '14 at 17:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.