I am fairly new to Mathematica. I've been playing around with ClusteringComponents. Assume we are clustering points in the plane using k-means clustering and EuclidianDistance as distance function:

data = RandomReal[10, {n, 2}];
clusters = ClusteringComponents[data, 2, 1, Method -> "KMeans", 
  "DistanceFunction" -> EuclideanDistance, "RandomSeed" -> 1]

Unfortunately, the function only returns the clusters assignments. Is there an easy way to pull out the centroids as well, or alternatively, is there another built-in function that accomplishes this?.

  • 2
    $\begingroup$ Take the mean of the points in each cluster. $\endgroup$ – David G. Stork Apr 25 '15 at 19:45
  • $\begingroup$ that would compute the centroids of clusters based on SquaredEuclideanDistance but I am using EuclideanDistance. Idealy, I don't want to recompute anything which is already being computed internally by the function. If I can pull this out somehow would be great. $\endgroup$ – Peter Apr 25 '15 at 22:25
  • 1
    $\begingroup$ @Peter "Since the arithmetic mean is a least-squares estimator, this also minimizes the within-cluster sum of squares (WCSS) objective." (en.wikipedia.org/wiki/K-means_clustering) and Chapter 9, Pattern Classification (2nd ed.) by Duda, Hart and Stork. $\endgroup$ – David G. Stork Apr 26 '15 at 2:19
  • $\begingroup$ yes, the arithmitic mean is a least squares estimator which corresponds using SquaredEuclideanDistance function (which k-means usually does). However, I am using Euclidean distance function (see code) in which case the centroids should correspond to the geometric median: link The fact that I can use this distance function in combination with k-means without error, leads me to believe that Mathematica is actually implementing a non-standard k-means algorithm. Given that the function is already computing this internally, can't I just pull it out? $\endgroup$ – Peter Apr 26 '15 at 15:31
  • $\begingroup$ @Peter You can't "pull out" stuff from a black box function, however in the question I linked to above there is an implementation of k-means that you can modify as you wish. $\endgroup$ – C. E. Apr 26 '15 at 17:54

Browse other questions tagged or ask your own question.