K-Means Clustering
K-Means Clustering is an unsupervised machine learning algorithm that partitions a dataset into k distinct, non-overlapping groups (clusters) by iteratively assigning each data point to its nearest cluster center and then recomputing those centers. It matters in modern AI because it underpins everything from customer segmentation and image compression to initializing more complex models and exploring unlabeled datasets at scale. A counterintuitive fact: K-Means is not guaranteed to find the globally optimal clustering — it can get "stuck" in a bad solution depending entirely on where the initial cluster centers are randomly placed.
K-Means works by alternating between two simple steps until nothing changes: assign every point to whichever cluster center it is closest to, then move each cluster center to the average position of all the points assigned to it. The "aha moment" is realizing that these two steps — assign and update — pull the cluster centers toward the natural density pockets of your data, even though you never told the algorithm what those pockets look like. The algorithm is self-organizing: it discovers structure purely from distances between points.
How It Works
K-Means minimizes a quantity called inertia (also called within-cluster sum of squares): the total squared distance from every point to its assigned cluster center. Lower inertia means tighter, more compact clusters.
Explanation: J is the total cost — sum over all n data points of the squared straight-line distance from each point xᵢ to its cluster's center μ.
Symbols:
- — the objective (inertia) we want to minimize
- — the -th data point (a vector of feature values)
- — the centroid of whichever cluster point is assigned to
- — squared Euclidean distance
The cluster center (centroid) update step computes a simple mean:
Explanation: The new position of centroid k is just the average coordinate of every point currently assigned to cluster k.
Symbols:
- — the new position of centroid
- — the set of all points currently assigned to cluster
- — the number of points in that set
The Full Algorithm, Step by Step
1. Choose k — decide how many clusters you want. 2. Initialize — randomly place k centroids (or use the smarter K-Means++ strategy). 3. Assign — label each data point with the index of the nearest centroid. 4. Update — move each centroid to the mean of its assigned points. 5. Repeat steps 3–4 until assignments stop changing (convergence).
Python Code Example
```python from sklearn.cluster import KMeans import numpy as np import matplotlib.pyplot as plt
# --- Generate some fake 2D data with 3 natural clusters --- np.random.seed(42) blob_1 = np.random.randn(50, 2) + [0, 0] blob_2 = np.random.randn(50, 2) + [5, 5] blob_3 = np.random.randn(50, 2) + [10, 0] X = np.vstack([blob_1, blob_2, blob_3])
# --- Fit K-Means with k=3 --- kmeans = KMeans(n_clusters=3, random_state=42, n_init=10) kmeans.fit(X) labels = kmeans.labels_ # cluster index for each point centroids = kmeans.cluster_centers_ # (3, 2) array of centroid positions inertia = kmeans.inertia_ # total within-cluster sum of squares print(f"Inertia: {inertia:.2f}") print(f"Centroids:\n{centroids}")
# --- Visualise --- plt.scatter(X[:, 0], X[:, 1], c=labels, cmap='viridis', alpha=0.6) plt.scatter(centroids[:, 0], centroids[:, 1], marker='X', s=200, c='red', label='Centroids') plt.legend() plt.title("K-Means Clustering (k=3)") plt.show() ```
Choosing k with the Elbow Method
```python inertias = [] k_range = range(1, 10) for k in k_range: km = KMeans(n_clusters=k, random_state=42, n_init=10) km.fit(X) inertias.append(km.inertia_) plt.plot(k_range, inertias, marker='o') plt.xlabel('Number of clusters k') plt.ylabel('Inertia') plt.title('Elbow Method — look for the "elbow" bend') plt.show()
# The elbow at k=3 tells us 3 is likely the right number of clusters ```
Stuart Lloyd was a mathematician at Bell Labs who developed the iterative centroid-refinement procedure in an internal technical memo in 1957, though the work was not formally published until 1982 in the IEEE Transactions on Information Theory. His original motivation was not machine learning but pulse-code modulation — a signal compression problem — making K-Means an algorithm that came from telecommunications, not statistics. A surprising historical fact: because Lloyd's memo circulated unpublished for 25 years, the algorithm was independently rediscovered multiple times, leading to the alternate names "Lloyd's algorithm" and the "Forgy method."
Intuition
Imagine you walk into a large party and need to form three conversation circles. You start by dropping three beach balls at random spots on the floor — those are your initial centroids. Every person at the party walks toward the beach ball closest to them and stands near it (the assign step). Now you pick up each beach ball and move it to the center of the crowd standing around it (the update step). People shuffle toward whichever ball is now closer to them, and you move the balls again. After a few rounds, the beach balls settle in the middle of three natural clusters of people and nobody moves anymore — that is convergence. The key insight is that you never told anyone which group to join: proximity alone organized the crowd.
Historical Development
Origins · 1 minhow this was discovered
Why It Matters
Prerequisites
What This Leads To
See Also
DBSCAN · Gaussian Mixture Models · Hierarchical Clustering · Principal Component Analysis · Silhouette Score · Elbow Method · Mini-Batch K-Means