K-Means Clustering
Limitations
Read aloud is unavailable in this browser.
When K-Means Fails
K-means is remarkably successful across diverse applications, but it has fundamental limitations that every practitioner must understand. Blindly applying k-means to data that violates its assumptions leads to meaningless clusters, wasted effort, and incorrect conclusions.
In this page, we'll systematically examine k-means' limitations—not as criticisms, but as a guide for when to use it and when to choose alternatives. Understanding these limitations is the mark of a mature machine learning practitioner: knowing not just how to apply a technique, but when to apply it.
By the end of this page, you will: • Identify data characteristics that cause k-means to fail • Understand why k-means finds spherical clusters only • Recognize the k selection problem and its solutions • Know when to choose alternatives (GMM, DBSCAN, spectral) • Apply practical guidelines for k-means applicability
The Spherical Cluster Assumption
K-means' most fundamental limitation stems from its objective function: minimizing within-cluster variance with squared Euclidean distance inherently assumes spherical, isotropic clusters.
Why Spherical?
The Voronoi tessellation created by k-means divides space using perpendicular bisectors between centroids. Each cluster region is a convex polytope. This geometry implies:
- Clusters are convex: K-means cannot discover non-convex shapes (crescents, rings, interleaved spirals)
- Equal variance assumed: All clusters are assumed to have similar spread
- Isotropic spread: Variance is equal in all directions (not elongated ellipses)
The "two moons" dataset (two interleaved crescents) is the canonical example where k-means fails:
• True structure: Two curved, non-convex clusters • K-means result: Splits horizontally through the middle, putting parts of each moon in both clusters • The problem: No Voronoi tessellation can separate curved regions
For such data, use spectral clustering or DBSCAN instead.
Elongated Clusters:
Even for convex clusters, k-means struggles with elongated shapes:
True clusters: K-means finds:
///// ||||| |
///// ||||| |
///// ||||| |
\\\\\ | |||||
\\\\\ | |||||
\\\\\ | |||||
K-means cuts perpendicular to the line connecting centroids. If clusters are elongated diagonally, the cut misaligns with the true boundary.
Solution: Use Gaussian Mixture Models (GMMs) with full covariance matrices, which can learn ellipsoidal clusters of different orientations.
| Cluster Shape | K-Means | GMM | DBSCAN | Spectral |
|---|---|---|---|---|
| Spherical, equal size | ✅ Ideal | ✅ Good | ✅ Good | ✅ Good |
| Spherical, unequal size | ⚠️ Struggles | ✅ Good | ⚠️ Needs tuning | ✅ Good |
| Ellipsoidal | ❌ Fails | ✅ Ideal | ⚠️ Depends | ✅ Good |
| Non-convex (moons) | ❌ Fails | ❌ Fails | ✅ Ideal | ✅ Good |
| Arbitrary density | ❌ Fails | ❌ Fails | ✅ Ideal | ⚠️ Depends |
The K Selection Problem
K-means requires specifying $k$ (number of clusters) upfront. This is often the most challenging aspect of using k-means:
- Too few clusters: Distinct groups get merged, losing information
- Too many clusters: Natural groups get split unnecessarily
- No "correct" k exists: The optimal k depends on the use case and scale of interest
Methods for Choosing K:
The Elbow Method
Plot WCSS (inertia) vs. k for k = 1, 2, ..., K_max. Look for an "elbow" where adding more clusters yields diminishing returns.
WCSS
|\
| \
| \_ <- Elbow around k=3
| \___________
|______________________ k
1 2 3 4 5 6 7
Pros:
- Simple and intuitive
- Widely understood
Cons:
- Elbow often ambiguous or doesn't exist
- Subjective interpretation
- WCSS always decreases with k (no natural stopping point)
In practice, combine multiple methods:
- Run elbow method to get a range (e.g., k ∈ [3, 7])
- Compute silhouette scores for that range
- Examine actual cluster interpretations for top candidates
- Let domain knowledge guide final choice
Often, the "right" k is the one that produces clusters you can explain and act on.
Sensitivity to Scale and Outliers
K-means is sensitive to both feature scales and outliers—problems that can produce meaningless clusters if not addressed.
Feature Scale Sensitivity:
Squared Euclidean distance treats all dimensions equally in magnitude: $$|\mathbf{x} - \boldsymbol{\mu}|^2 = \sum_{d=1}^{D} (x_d - \mu_d)^2$$
If feature 1 ranges [0, 1] and feature 2 ranges [0, 1000], feature 2 dominates completely:
- A difference of 0.1 in feature 1 contributes $0.01$
- A difference of 10 in feature 2 contributes $100$
Clusters are determined almost entirely by feature 2.
Before running k-means, standardize features to zero mean and unit variance:
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
kmeans = KMeans(n_clusters=k)
kmeans.fit(X_scaled) # NOT X
Alternatively, use min-max scaling to [0, 1]. The key is equal contribution from all features.
Outlier Sensitivity:
Because k-means minimizes squared distances, outliers have outsized influence:
- An outlier 10 units away contributes 100 to WCSS
- A normal point 1 unit away contributes 1
Centroids get "pulled" toward outliers, distorting cluster structure.
Example:
Points: 1, 2, 3, 4, 5, 100 (outlier)
Mean: (1+2+3+4+5+100)/6 = 19.2 ← Pulled far from majority
Median: 3.5 ← Robust to outlier
Solutions:
- Preprocessing: Detect and remove outliers before clustering
- K-medoids (PAM): Uses actual data points as centers, more robust
- K-medians: Uses L1 distance and median as center, robust to outliers
- Robust statistics: Use trimmed means or Huber loss
| Issue | Standard K-Means | Robust Alternative |
|---|---|---|
| Outliers | Mean centroid (sensitive) | K-medoids (actual data points) |
| Non-spherical clusters | Euclidean distance | Mahalanobis distance, kernel k-means |
| Mixed data types | Numeric only | K-prototypes, Gower distance |
| Large n | O(nkd) per iteration | Mini-batch k-means |
Equal Cluster Size Bias
K-means has an implicit bias toward producing equally-sized clusters. When true clusters have very different sizes, k-means tends to split large clusters and merge small ones.
Why This Happens:
The WCSS objective doesn't penalize unequal cluster sizes. But with equal-radius spherical clusters, more points means higher WCSS. The optimization trades off:
- Keeping small clusters pure (low WCSS contribution)
- Splitting large clusters (reducing their WCSS contribution)
The result: boundaries shift toward large clusters, stealing points to balance sizes.
Example:
True clusters: 1000 points (dense cloud) + 50 points (small group)
K-means with k=2 might:
- Split the large cloud into two pieces
- Assign the small group to one of those pieces
- Result: Two equally-sized but meaningless clusters
DBSCAN and HDBSCAN don't assume equal-sized clusters:
• Clusters are defined by density, not distance to centroids • Small, dense clusters are preserved alongside large ones • Noise points (outliers) are explicitly identified
For datasets with varying cluster sizes, density-based methods often produce more meaningful results.
Diagnosing the Problem:
After clustering, examine cluster sizes:
np.bincount(labels)
# array([250, 248, 252, 250]) ← Suspiciously equal
If cluster sizes are nearly equal despite expecting variation, k-means may be forcing balance.
Mitigation Strategies:
- Overcluster then merge: Use larger k, then merge based on similarity
- Use GMM: Gaussian mixtures naturally handle different cluster sizes/shapes
- Weight points differently: Underweight dense regions to reduce their influence
- Constrained k-means: Add size constraints (but computationally harder)
The Curse of Dimensionality
K-means' reliance on Euclidean distance makes it susceptible to the curse of dimensionality—the phenomenon where distance-based methods break down in high dimensions.
The Problem:
In high dimensions:
- Distance concentration: All pairs of points become nearly equidistant
- Nearest neighbor meaningless: The farthest point is barely farther than the nearest
- Empty space dominates: Most of the space is empty (hypersphere volume shrinks)
Mathematical Form:
For uniformly distributed points in $d$ dimensions: $$\lim_{d \to \infty} \frac{d_{\max} - d_{\min}}{d_{\min}} = 0$$
The relative contrast between distances vanishes—everything looks equally far.
The curse of dimensionality becomes problematic when: • $d > 20-50$ for random data • $d > n$ (more features than samples) • Features are mostly noise
For structured data (images, text embeddings), high-dimensional k-means can still work because the data lies on a lower-dimensional manifold.
Solutions for High Dimensions:
- Dimensionality Reduction First:
from sklearn.decomposition import PCA
pca = PCA(n_components=50) # Reduce to 50 dimensions
X_reduced = pca.fit_transform(X)
kmeans.fit(X_reduced)
-
Feature Selection: Remove irrelevant features before clustering
-
Subspace Clustering: Find clusters in different subspaces
-
Use Different Distance: Cosine similarity works better for high-dimensional sparse data
-
Spectral Clustering: Build similarity graph and cluster on eigenvectors (lower dimensional representation)
Rule of Thumb:
If $d > 50$ and k-means produces poor silhouette scores, consider:
- Reducing dimensions to 10-50 components via PCA
- Switching to cosine k-means for text/sparse data
- Using spectral clustering
When to Use K-Means (and When Not To)
Given all these limitations, when should you actually use k-means? Here's a practical decision framework.
- •Clusters are roughly spherical/convex
- •Clusters have similar sizes and densities
- •You know (or can estimate) k
- •Data is numeric and continuous
- •Features are properly scaled
- •Speed is important (large n)
- •You need a simple, interpretable baseline
- •Clusters have non-convex shapes
- •Clusters have very different sizes
- •Many outliers present
- •Number of clusters is unknown
- •Data has mixed types (categorical + numeric)
- •High-dimensional with noise
- •Cluster boundaries are fuzzy
| Scenario | Recommended Algorithm |
|---|---|
| Well-separated spherical clusters | K-Means |
| Ellipsoidal clusters, varying sizes | GMM |
| Arbitrary shapes, density-based | DBSCAN / HDBSCAN |
| Non-convex, need k clusters | Spectral Clustering |
| Hierarchical structure desired | Agglomerative Clustering |
| Very high dimensionality | PCA + K-Means or Spectral |
| Mixed data types | K-Prototypes |
Always start with k-means as a baseline—it's fast and often "good enough."
- Standardize features
- Run k-means with several k values
- Evaluate with silhouette score and visual inspection
- If clusters are poor (low silhouette, non-spherical shapes), switch algorithms
- Compare alternatives on the same metrics
K-means failing is informative—it tells you the data has complex structure worth modeling.
Summary: K-Means Limitations
- •K-means assumes spherical clusters — Cannot discover non-convex shapes or elongated ellipses
- •K must be specified upfront — Use elbow, silhouette, or gap statistic to estimate
- •Sensitive to scale and outliers — Always standardize; consider k-medoids for outliers
- •Bias toward equal cluster sizes — May split large clusters and merge small ones
- •Struggles in high dimensions — Apply PCA or use cosine distance for high-d sparse data
- •Use k-means as a baseline — Fast and simple; switch to GMM/DBSCAN/spectral when it fails
Module Complete:
Congratulations! You've completed the K-Means Clustering module. You now understand:
- Lloyd's algorithm: The iterative assignment-update procedure
- The objective function: WCSS and its mathematical properties
- Initialization: K-means++ and its O(log k) guarantee
- Convergence: Proofs, rates, and stopping criteria
- Limitations: When k-means works and when to choose alternatives
This knowledge prepares you for the k-means variants module (k-medoids, fuzzy c-means, mini-batch) and for understanding how other clustering algorithms address k-means' limitations.
You have mastered k-means clustering from algorithm to implementation to practical deployment. You understand its mathematical foundations, can implement it correctly, and know when to use alternatives. This is the gold standard of clustering knowledge that will serve you throughout your machine learning career.
KEEP LEARNING
There’s more to understand.
You’re reading the introduction. Create a free account to read the complete lesson, save notes and keep track of your progress.