SproutStackSproutStack home

Clustering with K-Means

5 min readBeginner-friendlyFree to read

Read a little, play a little. No scary maths, and no rush.

Share:

Finding groups nobody labeled

No churn column, no spam flags — just customers described by spending, visits, tenure. Clustering discovers natural groupings anyway: bargain hunters, loyal regulars, about-to-leave. It's exploration, not prediction — the output is insight ("oh, there are four kinds of users"), which then drives decisions, features, or targeted models per group.

How K-means thinks

Pick K center points. Repeat: assign every point to its nearest center, then move each center to its group's middle. Stop when centers stop moving. The result: K blobs minimizing within-group distance.

from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans

Xs = StandardScaler().fit_transform(X)  # scale FIRST — see below
labels = KMeans(n_clusters=4, n_init=10, random_state=7).fit_predict(Xs)

The two things beginners get wrong

  • Unscaled features. Income (tens of thousands) vs visits (single digits): raw distance is 99% income. Standardize first, always, for any distance-based method.
  • Treating K as truth. K-means always returns K groups, even from pure noise. Try several K values, elbow-plot the inertia (the "bend" suggests a natural count), and — critically — validate clusters mean something in the business, not just the math. A segment nobody can act on is decoration.

Watching K-means converge

Take six values on a line: 1, 2, 3, 10, 11, 12, with K = 2, and suppose the centers start badly at 1 and 2.

  • Assign: 1 goes to the first center. Everything else is closer to 2, so the second group is {2, 3, 10, 11, 12}.
  • Move: the first center stays at 1; the second moves to the average of its group, 7.6.
  • Assign again: 2 and 3 are now closer to 1 than to 7.6, so the groups become {1, 2, 3} and {10, 11, 12}.
  • Move again: the centers settle at 2 and 11. Nothing changes on the next round, so it stops.

Two rounds turned a poor start into the obvious answer. With bad luck, a start can also settle somewhere worse, which is why n_init=10 runs the whole thing ten times from different starts and keeps the best.

A second way to choose K

The silhouette score measures how well each point sits inside its own cluster compared with the nearest other cluster, from −1 to 1, higher being better. Compute it for several K values alongside the elbow plot. When both point to the same K, you can be reasonably confident; when they disagree, let business usefulness decide.

When K-means is the wrong tool

K-means assumes roughly round clusters of similar size, and every point must belong somewhere. It struggles with long or curved shapes, groups of very different density, and outliers, which drag centers toward them. DBSCAN finds clusters of any shape and labels isolated points as noise. Hierarchical clustering builds a tree of groupings you can cut at whatever level of detail you need.

Making clusters useful

Profile each cluster with simple averages, such as spend, visits, and tenure, and give it a plain name like "weekend bargain hunters." Rerun with different random seeds to check that the groups stay stable. A segment that reshuffles every run is not a real pattern.

Remember this

  • No labels → cluster for structure; scale first; choose K by elbow + business sense.
  • Clusters are hypotheses to validate, not answers to ship blindly.

Check your understanding

5 questions · first-try answers earn the most XP

1. Why must features be standardised before running K-Means?
2. You run K-Means with K = 7 on pure random noise. What happens?
3. What does the elbow plot help you decide when choosing K for K-Means?
4. Why does the chapter say a cluster nobody can act on is decoration?
5. What is the main practical use of clustering when you already have labelled data?

My notes

Saved in this browser. Highlight a line above and save it, or write it in your own words.

Nothing saved yet. Your highlights will live here.

References

Finished reading?

Ticking it here also ticks the chapter in the sidebar, the section count and your streak — it is all one number.

Related chapters

Spotted a mistake or want a topic covered? Report an issue

📬 An occasional email when new lessons land

New lessons and showdowns, no more than a few times a month. No spam, unsubscribe anytime. We store your email only to send the newsletter.