Choosing Where K-Means Starts

Classic MLMedium+20 XP+20

Choosing Where K-Means Starts

K-Means drops its KKK starting centroids at random, and an unlucky drop can leave it stuck with a poor clustering, for example when two centroids land inside the same natural group. K-Means++ is a smarter way to choose the starting centroids. It still uses randomness, but it tilts the odds toward points that are far from every centroid chosen so far.

The recipe, for a list of points and k clusters:

  1. First centroid: pick one point, every point equally likely.
  2. Every next centroid:
    • For each point, find its squared straight-line distance to the nearest centroid chosen so far. Call it D2D^2D2. A point that is already a centroid has D2=0D^2 = 0D2=0.
    • Pick one point at random, with each point's chance proportional to its D2D^2D2: a point with twice the D2D^2D2 is twice as likely to be picked.
  3. Repeat step 2 until k centroids have been chosen.

So that everyone gets the same answer, the random numbers have been drawn in advance. draws holds k numbers, each at least 0 and less than 1. Use them like this:

  • First centroid: the point at index int(draws[0] * len(points)).
  • Centroid number ccc (for c=1,2,…c = 1, 2, \dotsc=1,2,…): add up every point's D2D^2D2 to get a total SSS, and set a target of draws[c] ×S\times S×S. Then go through the points in order, keeping a running sum of their D2D^2D2 values, and pick the first point whose running sum is greater than the target.

Task: write kmeans_pp_starts(points, k, draws) and return the indices of the chosen points, in the order they were chosen.

  • Every point is a list of coordinates, and all points have the same number of them.
  • The points always include at least k different positions, so there is always a valid next centroid.

The running sum is the standard trick for drawing at random with uneven chances. Lay all the D2D^2D2 values end to end along a line of length SSS, drop a pin at draws[c] ×S\times S×S, and take whichever point's stretch the pin lands in. Far-away points own long stretches, so they are hard to miss.

Examples

points = [[0, 0], [1, 0], [0, 1], [6, 0], [6, 1], [3, 8]]
k = 2
draws = [0.0, 0.3]
returns [0, 4]
points = [[0, 0], [1, 0], [5, 5], [6, 5], [0, 9], [1, 9]]
k = 3
draws = [0.7, 0.2, 0.85]
returns [4, 0, 3]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run