DEV Community

Nishant Banginwar
Nishant Banginwar

Posted on

K-Means Doesn't Find Your Clusters. It Finds Its Own.

Classic Machine Learning Through the Eyes of an SRE — Part 5

Supervised learning at least tells you when it's wrong. Unsupervised learning will happily hand you a confident answer to a question your data never contained.

K-Means is where that lesson starts, because K-Means always gives you the K clusters you asked for. Ask it for 5 clusters of your client accounts and you will receive exactly 5 clusters, with centroids and an objective value. Whether your accounts actually form 5 groups — or any groups at all — is not something the algorithm can tell you.

It answers the question "what's the best way to split this into 5 compact groups?", not "are there 5 groups here?"

Those are very different questions. Only one of them was yours.

The bet it makes

Two bets, actually.

Round blobs. Assigning each point to its nearest centroid creates a Voronoi partition — convex cells with straight boundaries. Pair that with an objective that minimizes squared distance to the center and you get a strong preference for compact, roughly isotropic groups.

If your real segments are elongated, chained, or density-based — say, a "slow-burn escalation" pattern that snakes across metrics — K-Means will slice that snake into three neat blobs and report success.

That tendency toward compact, similarly scaled groups deserves its own warning. If one segment is enormous and another is a handful of accounts, the distance-minimization objective can produce unintuitive partitions. Nothing in the output tells you that the resulting groups correspond to meaningful business segments.

You know K. The number of clusters is an input, not an output. The elbow method and silhouette scores help you argue about it, but they're heuristics, not oracles. K is your hypothesis about the world, wearing the algorithm's clothes.

SEARCH, not solve

Mechanically, K-Means iterates: place centroids, assign points, move centroids to the mean, repeat until convergence. That's Lloyd's algorithm, and it's a heuristic — the global K-Means optimization problem is NP-hard in general, so what you get is a local optimum that can depend on initialization.

Run it with different initializations, and you can get different clusters. k-means++ initialization and multiple restarts manage this, but "manage" is the honest verb.

It's worth naming what's actually being minimized: the within-cluster sum of squares, usually called inertia. And there's a catch in it that explains why the elbow method has to exist at all.

The optimal inertia never increases as K rises — at K = n, every point is its own cluster and inertia is zero. So you cannot choose K by simply minimizing the objective. The elbow exists precisely because the metric can't answer the business question, which leaves you looking for the point where additional complexity stops being worth the improvement.

That word SEARCH matters, and it's my own shorthand rather than standard terminology, for how each unsupervised algorithm arrives at an answer.

  • K-Means SEARCHES: iterate and hope.
  • DBSCAN DEFINES: declare a density rule and traverse what follows.
  • PCA SOLVES: an eigendecomposition/SVD gives a direct solution rather than an iterative local search.
  • Hierarchical BUILDS: greedy merges, all the way up.

Four strategies for finding answers without labels, and K-Means is the anchor I compare the rest against — most of what they do is best understood as a reaction to something K-Means gets wrong.

The failure that hides

With no labels, there's no accuracy metric to catch you. The failure signature isn't an error — it's a plausible-looking segmentation that quietly mismatches reality.

Concrete version from my world: cluster IT-services accounts for a QBR deck. K-Means produces "5 client segments." The deck ships. Strategy gets built on those segments.

Nobody ever asks whether K=5 actually represents meaningful structure or whether the clusters cut across every important business boundary — because the output looks like insight, and there's no ground truth to embarrass it.

In supervised land, a bad model gets caught by the test set.

In unsupervised land, the test set is a stakeholder meeting three months later.

What I'd tell my ops team

Treat K-Means output as a HYPOTHESIS generator, not a report.

Before anything downstream consumes the clusters:

  • Check stability — do the clusters survive resampling and re-initialization?
  • Check the geometry — look at silhouette scores per cluster, not just the average.
  • Name each cluster in business language — if a cluster can't be described in one sentence a delivery head recognizes, it may be an artifact rather than a useful segment.

And scale your features first. Like SVM, K-Means runs on distance, so unscaled features can silently decide the clustering for you.

The supervised block taught me that the threshold is a business decision. The unsupervised block starts with a harder version: sometimes the QUESTION is a business decision.

K-Means will answer whatever K you hand it. Choosing K responsibly is your job, not its.

Production takeaway

Treat clusters as a hypothesis, never a report.

Before anything downstream consumes them: test stability across resamples and re-initialization, read silhouette scores per cluster rather than only the average, and name each cluster in one sentence a delivery head would recognize.

If it can't be named, it's an artifact.

Common interview mistake

Presenting K as something the algorithm discovered. K is your input and your hypothesis; the elbow and silhouette are arguments, not oracles. Worth knowing why: the optimal inertia never increases as K rises, so the objective itself can never choose K for you.

Second, describing Lloyd's algorithm as if it finds the globally optimal clustering. The K-Means optimization problem is NP-hard in general, so Lloyd's converges to a local optimum depending on initialization. Multiple initializations and k-means++ reduce that sensitivity without changing the underlying problem.

Third, and easiest to miss: skipping feature scaling on a distance-based algorithm.

Where I'd use this in a real production system

Account or customer segmentation for coverage models · workload profiles for capacity planning · a first-pass structure check before designing a supervised label.

Series: what ML algorithms bet about your world, production/ops lens. Previous: SVM. Next: DBSCAN — the first algorithm with no loss function at all.

Top comments (0)