K-Means¶
What This Is¶
K-Means partitions n points into k clusters by alternating two steps:
- assign each point to the nearest cluster center
- recompute each cluster center as the mean of its assigned points
The objective being minimized is the within-cluster sum of squares (WCSS, also called inertia): Σ_i ||x_i - μ_{c(i)}||².
The practical lesson is that K-Means answers "which k compact, roughly equal-sized, spherical clusters does this data look like?" — and only that question. The moment the clusters are not spherical, not equal-sized, or not compact in Euclidean distance, K-Means imposes its assumptions anyway and gives you a confidently wrong answer.
When You Use It¶
- fast exploratory clustering of tabular data with continuous scaled features
- quantization — producing a small codebook of prototype vectors
- initializing more expensive unsupervised methods
- a structure-check before hand-labeling — if K-Means and your intended labels disagree, one of them is worth questioning
Do Not Use It When¶
- clusters have very different sizes or densities
- clusters are elongated, curved, or non-convex — use DBSCAN or spectral clustering instead; see Advanced Clustering and Dimensionality Reduction
- features are unscaled or categorical — the Euclidean objective is meaningless
- you need a probabilistic cluster membership — a Gaussian Mixture Model is the right tool
- the data is very high-dimensional — reduce first (see PCA)
The Algorithm¶
1. initialize k centers (k-means++ is the standard initializer)
2. repeat until centers stop moving:
a. for each point x_i: c(i) = argmin_k ||x_i - μ_k||^2
b. for each cluster k: μ_k = mean({x_i : c(i) = k})
Convergence is guaranteed to a local minimum of WCSS — not the global minimum. That is why n_init (number of independent restarts) matters.
Tooling¶
KMeansMiniBatchKMeansfor large datasetsn_clusters=kinit="k-means++"(default; almost always the right choice)n_init— number of random restarts (scikit-learn default isn_init="auto", typically 10)max_iterrandom_stateinertia_— WCSS after fittingcluster_centers_labels_silhouette_scorefor cluster qualityStandardScalerin a pipeline
Choosing k¶
Two honest tools for choosing k:
The elbow method¶
Run K-Means for k = 1, 2, ..., 10 and plot inertia_ vs. k. Inertia always decreases — but the rate of decrease usually flattens after the "right" k. The elbow is where extra clusters stop being worth it. The elbow is a heuristic, not a theorem; it often shows but is not always sharp.
Silhouette score¶
For each point, silhouette = (b - a) / max(a, b) where a is mean distance to other points in the same cluster and b is mean distance to the nearest other cluster. Average over all points. Higher is better; negative values indicate points that would be better in a different cluster.
Combine both. Run the elbow for a fast first guess, confirm with silhouette at the elbow and one k on either side.
Scaling And Initialization¶
Two non-negotiable hygiene moves:
- always scale before K-Means — a dollar feature and a percent feature cannot share a Euclidean distance
- always run multiple initializations — a single restart can land on a bad local minimum;
n_init=10is the standard default
Minimal Example¶
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans
model = make_pipeline(StandardScaler(), KMeans(n_clusters=4, n_init=10, random_state=0))
model.fit(X)
labels = model.predict(X)
The random_state gives you a reproducible answer. K-Means with a floating seed produces different cluster IDs on every run (though usually the same partitions).
What To Inspect¶
- the elbow curve — where does inertia stop dropping meaningfully
- the silhouette score at the chosen
k - cluster sizes — are any clusters tiny or huge
- a 2D projection (PCA or UMAP) colored by cluster — do the clusters look separable there
- cluster centers on the original feature scale — what do they actually mean
- whether the partition changes across
random_statevalues — if yes, the clustering is unstable
Failure Pattern¶
Running K-Means on unscaled features and reporting the clusters as a finding. The biggest-variance feature is almost certainly doing all the work of distance.
A second failure pattern is treating K-Means output as a ground truth instead of a hypothesis. K-Means will always give you k clusters; whether those k clusters mean anything is a separate question you have to answer with silhouette, cluster stability, or domain interpretation.
A third failure pattern is mistaking "K-Means converged" for "K-Means found the right answer." Convergence to a local optimum is a convergence to some optimum, not the best one.
Quick Checks¶
- Is the data scaled before K-Means?
- Is
n_initat least 10? - Is
random_statefixed? - Did you look at the elbow or silhouette score, not just pick
k=3by habit? - Do the clusters survive a different
random_state?
Practice¶
- Generate three isotropic Gaussian blobs and confirm K-Means recovers them.
- Generate two elongated blobs along a diagonal axis and watch K-Means fail. Explain why.
- Sweep
kfrom 2 to 10 and plot inertia. Identify the elbow. - Compute silhouette scores for
k = 2, 3, 4, 5on the same data and compare to the elbow. - Run K-Means twice with different
random_statevalues. Are the clusters stable? - Use
MiniBatchKMeanson a dataset with 100k rows and compare timing and result quality to full K-Means. - Explain why scaling matters and why
k-means++is a better initializer than random. - Describe one case where DBSCAN or a Gaussian mixture model would be a better fit.
- State the relationship between K-Means and the Gaussian mixture model with shared identity covariance.
- Explain why K-Means always has a well-defined but often useless answer on categorical data.
Longer Connection¶
K-Means sits next to:
- Clustering and Low-Dimensional Views — the surrounding workflow that puts clustering to use
- Advanced Clustering and Dimensionality Reduction — non-convex clustering methods for when K-Means' assumptions break
- PCA — the usual reduction before high-dimensional clustering
- Dimensionality Reduction — the broader workflow of getting distances to mean something again
K-Means is a hypothesis machine. It tells you what the data looks like if the clusters are spherical, equal-sized, and compact. The next question is always whether that assumption matches reality.