K-Means++
Algorithm Implementation
Machine Learning
Clustering
Data Science

How Could One Implement the K-Means Algorithm?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

K-Means++ Algorithm: A Structured Implementation Guide

K-Means++ is an enhanced version of the traditional k-means clustering algorithm. It improves upon the initialization step of the k-means algorithm, which helps in overcoming the poor cluster initialization often experienced with k-means. By ensuring that the initial centroids are spaced apart, k-means++ tends to converge faster and lead to better clustering results.

Here is a step-by-step guide on how one can implement the K-Means++ algorithm:

Step-by-Step Implementation

Step 1: Initialization

  1. Choose the first centroid randomly from the data points (X) and add it to the set of chosen centroids.
  2. Calculate the distance (D) from each remaining data point to its nearest centroid. The squared distance is often used as it emphasizes larger distances more: D(x) = min_{c ∈ C} ||x - c||^2.
  3. Choose subsequent centroids from the remaining data points with a probability proportional to D(x), using the weighting P(x) = D(x) / ∑_{x ∈ X} D(x).
  4. Repeat Step 3 until K centroids are chosen.

Step 2: Assigning Clusters

  1. For each data point, assign it to the nearest centroid. This can be achieved by calculating the Euclidean distance from the point to each centroid and choosing the smallest value.

Step 3: Update Centroids

  1. After assigning all data points to the nearest clusters, update the cluster centroids by calculating the mean of all points assigned to each cluster: c_k = (1 / |C_k|) ∑_{x ∈ C_k} x.

Step 4: Iterate until Convergence

  1. Repeat Step 2 and Step 3 until the centroids do not change significantly, or a fixed number of iterations are completed.

Implementation Example

Let’s consider a Python-based implementation using NumPy:

python
1import numpy as np
2from sklearn.metrics import pairwise_distances_argmin
3
4def kmeans_plus_plus(X, n_clusters, n_init=10, max_iter=300, tol=1e-4):
5    n_samples, n_features = X.shape
6    best_inertia = np.inf
7
8    for _ in range(n_init):
9        # Step 1: Initialize centroids using k-means++
10        centroids = np.zeros((n_clusters, n_features))
11        centroids[0] = X[np.random.randint(0, n_samples)]
12        
13        for i in range(1, n_clusters):
14            distances = np.min(pairwise_distances_argmin(X, centroids[:i]) ** 2, axis=1)
15            probabilities = distances / distances.sum()
16            cumulative_probabilities = np.cumsum(probabilities)
17            r = np.random.rand()
18
19            for j, p in enumerate(cumulative_probabilities):
20                if r < p:
21                    centroids[i] = X[j]
22                    break
23
24        # Step 2 & 3: Iterate over clustering
25        for _ in range(max_iter):
26            labels = pairwise_distances_argmin(X, centroids)
27            new_centroids = np.array([X[labels == j].mean(axis=0) for j in range(n_clusters)])
28
29            # Step 4: Check for convergence
30            if np.all(abs(new_centroids - centroids) < tol):
31                break
32            centroids = new_centroids
33
34        # Compute inertia
35        inertia = np.sum((X - centroids[labels]) ** 2)
36        
37        # Keep best solution
38        if inertia < best_inertia:
39            best_inertia = inertia
40            best_centroids = centroids
41            best_labels = labels
42
43    return best_centroids, best_labels

Performance Comparisons

Here's a table summarizing the key differences and benefits of using K-Means++ over the traditional K-Means:

AspectK-MeansK-Means++
InitializationRandom selection of centroidsSmart and spaced-out initialization
Convergence SpeedSlower due to poor initializationFaster due to better initialization
Cluster QualityHighly variable, initial dependentMore consistent, higher quality
ComplexityO(nkt) where k = number of clusters, t = iterations, n = samplesSlightly higher, for initialization but a consistent gain in quality

Conclusion

The K-Means++ algorithm provides a significant improvement over the standard k-means algorithm by improving the initialization of centroids. Its primary advantage is its ability to find a better set of starting points, reducing both the time of algorithm convergence and improving the final clustering performance. Implementers can benefit from a more reliable and efficient clustering approach by applying K-Means++.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.