Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I came across a variant of k-means that adds a algorithm to select good starting values called k-means++.

kMeansInitializer[data_, k_Integer] :=
  {startingPoint = RandomChoice[data], getDistance, getDistances, 
  getDistance[datum_, points_] := Min[Norm[datum - #]^2 & /@ points];
  getDistances[points_] := getDistance[#, points] & /@ data;
  nextPoint[points_] := RandomChoice[getDistances[points] -> data];
  NestList[nextPoint, startingPoint, k - 1]

After implementing the algorithm (a bit of a challenge for someone at my level) I now find I have no idea how to feed the values I generate to ClusteringComponents to use as initial values for use in its k-means implementation.

share|improve this question
I don't think it is possible to do that via ClusteringComponents. Here's an implementation of k-means that I wrote, which might be easily extended to your needs. – R. M. Oct 3 '12 at 23:33
up vote 0 down vote accepted

Posting rm -rf's comment as an answer for the purpose of improving the stats of this site, it is not possible to provide ClusteringComponents with initial values. It is almost certainly already implemented using an algorithm to choose good starting points, and probably because they all converge to the same solution it was not deemed necessary to make it possible to change the starting points.

But as rm -rf said he has implemented the most common algorithm for k-means and made it available here. You can provide your own starting points by changing

m = RandomSample[list, k]


m = kMeansInitializer[list, k]
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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