ML0058 K-means++

Please explain how K-means++ works.

Answer

K-means++ is an improved way to initialize the centroids of K-means. Instead of picking all centroids uniformly at random, it selects them one by one with probability proportional to the squared distance from the already chosen centroids. This spreads the initial centroids out, sharply reducing the chance of poor clustering and helping the algorithm converge faster and more reliably. After initialization, standard K-means proceeds unchanged.

(1) First Centroid: Choose \mu_1 uniformly at random from the dataset.
(2) Distance Computation: For each point, compute its squared distance to the nearest already-chosen centroid.
(3) Weighted Selection: Pick the next centroid with probability proportional to that squared distance: far points are favored.
(4) Repeat Until K: Continue until all K centroids are chosen, then run standard K-means.

Five clusters with centroids initialized by K-means plus plus landing one per cluster

Figure 1: K-means++ initialization (K=5): the distance-squared weighting spreads the starting centroids across all five natural groups, so the algorithm starts close to a good solution instead of gambling on random seeds.

Mathematical Formulation:
D(x_i)^2 = \min_{1 \le j \le m} \|x_i - \mu_j\|^2
P(x_i) = \frac{D(x_i)^2}{\sum_j D(x_j)^2}

Where:

  • \mu_j is one of the m already chosen centroids; D(x_i)^2 is point x_i‘s squared distance to its nearest chosen centroid.
  • P(x_i) is the probability of x_i becoming the next centroid, proportional to D(x_i)^2, so points far from every chosen centroid are exponentially favored over nearby ones.
  • The denominator normalizes over all data points; selection repeats until K centroids exist.
Side by side comparison where random initialization merges two groups and splits one while K-means plus plus finds all three true clusters

Figure 2: Why initialization matters: with an unlucky random init (left), two centroids land in one group and the small top cluster is merged away; K-means++ (right) seeds one centroid per group and recovers all three clusters cleanly.


Login to view more content


Log in to track your progress

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *