How to get the centroids of the results of the ClusteringComponents function?

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?

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"}]]


• 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. Sep 24, 2014 at 21:09

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]}]]


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]}]
`

• 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... Sep 27, 2014 at 17:20