K-Means
Clustering
Lloyd's Algorithm
Forgy's Method
Hartigan-Wong Algorithm

K-Means Lloyd,Forgy,MacQueen,Hartigan-Wong

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 Clustering is a fundamental unsupervised learning technique used for partitioning a dataset into a set of distinct, non-overlapping subsets or clusters. Each cluster features data points more similar to one another than to those in any other cluster. Over the years, several algorithmic implementations and enhancements of K-means have emerged, including those by Lloyd, Forgy, MacQueen, and Hartigan-Wong. This article delves into these implementations, their nuances, and technicalities.

K-Means Algorithm Overview

K-Means aims to partition nn observations into kk clusters wherein each observation belongs to the cluster with the nearest mean, serving as a prototype of the cluster. Here’s how the algorithm generally works:

  1. Initialization: Choose kk initial centroids, typically picked randomly.
  2. Assignment Step: Assign each data point to the nearest centroid, forming kk clusters.
  3. Update Step: Recompute the centroid of each cluster.
  4. Iteration: Repeat the assignment and update steps until convergence (i.e., centroids don't change significantly).

Convergence is usually measured by minimizing an objective function, typically the sum of squared distance between data points and their corresponding cluster centroids.

Variants of K-Means

1. Lloyd's Algorithm

Developed by Stuart Lloyd in 1957 during his Bell Labs days, Lloyd's algorithm is arguably the most common K-Means method used:

Advantages: Simple, intuitive, and widely adopted for practical clustering tasks. • Limitations: Can converge to local minima; performance depends on the initial centroids, leading to potential variations in results.

2. Forgy's Method

Published independently by Edward Forgy in 1965, the Forgy method is another common approach for k-means clustering. The key difference lies in choosing the initial centroids:

Initialization: Randomly select kk observations from the dataset as initial centroids. • Advantages: Simple initialization; often yields better initial centroids than random guesses. • Limitations: Similar to Lloyd’s, the final result can be sensitive to the initial centroids.

3. MacQueen's Method

James MacQueen introduced his version of information loss minimization in 1967. MacQueen's K-Means incorporates an incremental updating mechanism, which differentiates it from other methods:

Approach: Assign each data point to a cluster, then update clusters after the insertion of each point. • Advantages: Can converge faster because it adapts incrementally; potentially more stable with noisy data. • Limitations: Computationally more expensive than batch-wise updates since centroids are updated more frequently.

4. Hartigan-Wong Algorithm

JR Hartigan and MA Wong introduced this algorithm in 1979, which is known for its robustness and widespread usage in software implementations like R:

Mechanism: Instead of a simple reassignment, checks the potential for improved sum of squares before reassignment, minimizing the within-cluster variance more aggressively. • Advantages: Often converges faster and to better results than vanilla implementations due to iteratively reducing the objective function. • Limitations: Computationally more demanding; can be complex to implement properly.

Technical Analysis and Examples

Cost Function

The primary cost function used in K-Means is the inertia function, defined as:

Inertia(C)=i=1kxCixμi2\operatorname{Inertia}(C) = \sum_{i=1}^k \sum_{\mathbf{x} \in C_i} || \mathbf{x} - \mu_i ||^2

where CC is the set of all clusters, CiC_i is cluster ii, x\mathbf{x} is a data point, and μi\mu_i is the centroid of cluster ii.

Example

Given a synthetic dataset consisting of 2D points, applying K-Means with these variants can reveal differences in centroid trajectories and convergence speed. Below is a simplified example:


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.