What is clustering?
So far, many algorithms you’ve seen are given the right answers during training (that’s supervised learning). Clustering is different: we only have raw data and we ask the computer to find natural groups in it by itself. This is unsupervised learning.
Example: an online shop has data on thousands of customers — age, income and how much they spend. Nobody has labelled them. Clustering might discover groups like “young bargain-hunters”, “high-spending professionals” and “occasional buyers” — and the shop can target each group differently.
In the 3D model each dot is a customer and each axis is one feature. Points that are close together are similar.
The K-Means algorithm
You choose k, the number of groups you want. Then:
- Initialise: pick k starting points called centroids (the ◆ shapes).
- Assign step: every point joins the cluster of its nearest centroid (straight-line distance).
- Update step: move every centroid to the mean (average position) of the points in its cluster.
- Repeat steps 2 and 3 until no point changes cluster.
That’s it! In the model, the thin lines show which centroid each point belongs to. Watch the centroids glide towards the centre of their groups.
Measuring quality: WCSS
How good is a clustering? K-Means tries to minimise the Within-Cluster Sum of Squares (WCSS, also called inertia): the total squared distance from every point to its centroid.
WCSS = Σ over clusters Σ over points in cluster ‖point − centroid‖²
Both steps can only lower WCSS (or keep it equal), so the algorithm is guaranteed to stop. The model shows WCSS dropping after every update.
Choosing k — the elbow method
K-Means can’t tell you how many groups there should be. A popular trick: run it for k = 1, 2, 3, … and plot WCSS. The curve drops sharply at first and then flattens. The “elbow” where it bends is usually a good k.
Problems and fixes
| Problem | Why | Fix |
|---|---|---|
| Different results on each run | Random starting centroids | Run several times and keep the lowest WCSS; use k-means++ |
| Bad start → bad result | Two centroids start inside the same blob | k-means++ spreads the starting centroids apart |
| Features on different scales | Income (thousands) dominates age (tens) | Standardise features first |
| Weird-shaped clusters | K-Means assumes round, similar-sized blobs | Use DBSCAN or Gaussian Mixture Models |
Code
From scratch with NumPy:
import numpy as np
def k_means(X, k, iterations=100):
rng = np.random.default_rng()
centroids = X[rng.choice(len(X), k, replace=False)] # random start
for _ in range(iterations):
# assign: distance from every point to every centroid
dists = np.linalg.norm(X[:, None, :] - centroids[None, :, :], axis=2)
labels = dists.argmin(axis=1)
# update: mean of each cluster (keep old centroid if a cluster is empty)
new = np.array([X[labels == j].mean(axis=0) if np.any(labels == j) else centroids[j]
for j in range(k)])
if np.allclose(new, centroids):
break # converged
centroids = new
return labels, centroids
X = np.vstack([np.random.randn(30, 3) + c for c in ([0, 0, 0], [6, 6, 0], [0, 6, 6])])
labels, centroids = k_means(X, k=3)
print(centroids.round(2))
In practice you would use scikit-learn, which uses k-means++ by default:
from sklearn.cluster import KMeans
model = KMeans(n_clusters=3, n_init=10).fit(X)
print(model.labels_[:10]) # cluster of the first 10 points
print(model.inertia_) # WCSS
Where is K-Means used?
- Customer segmentation for marketing.
- Image compression: cluster the colours of an image into k colours.
- Document grouping: news articles about similar topics.
- Anomaly detection: points far from every centroid are suspicious.
- As a first step for other algorithms (e.g. initialising other models).
Common mistakes
- Forgetting to scale the features.
- Treating cluster numbers as meaningful: “cluster 2” is just a name; it can change between runs.
- Expecting K-Means to find long, curved or very unequal clusters — it prefers compact round ones.
Complexity at a glance
| Case / operation | Time | Why |
|---|---|---|
| One iteration | O(n · k · d) | n points × k centroids × d features distances. |
| Whole algorithm | O(i · n · k · d) | i iterations — usually small (tens). |
| Extra space | O(n · d + k · d) |
Quick check
Test yourself — pick an answer to see if you got it.
1. K-Means is an example of which kind of learning?
The data has no labels. K-Means discovers the groups on its own.
2. In the update step, where does each centroid move?
The new centroid is the average of its cluster's points — that's where the name "k-means" comes from.
3. When does K-Means stop?
If the assignments don't change, the centroids won't move either — the algorithm has converged.
4. What is the purpose of k-means++?
Spreading the starting centroids apart leads to better results and faster convergence on average.