Beyond AveragesScience in practice

Customer segmentation: k-means clustering step by step

Interactive k-means clustering demo for segmenting policyholders and investors: step through Lloyd's algorithm from k-means++ seeds, see why features must be standardised, choose k with the elbow and silhouette charts, test it on data with no real segments, and read a segment profile table.

Segmentation is the most common unsupervised-learning task in financial services: group policyholders or investors into a handful of segments that behave alike, then tailor pricing, marketing, service and risk appetite to each. k-means is the usual first tool. It places k centres and alternates between assigning every customer to the nearest centre and moving each centre to the mean of its customers, until nothing changes. It always returns k segments, whether or not the data contain any.

This module lets you watch that happen one iteration at a time, from k-means++ starting points, on three synthetic books: policyholders described by annual premium and claims frequency (four compact groups, the case k-means was built for), investors described by portfolio size and trading activity (three groups of very unequal size, one long and tilted), and a "No real segments" book drawn from a single population. Turn off standardisation and the lesson is immediate: with one feature in dollars and the other in counts, the dollar axis carries almost all of the raw variance and k-means simply cuts the book into premium bands.

Choosing k is a judgement call that the elbow chart (within-cluster sum of squares against k) and the average silhouette score inform but do not settle. On the policyholder book both point to the true four groups (silhouette about 0.70). On the no-segments book k-means still draws tidy, confidently labelled segments, but the silhouette never exceeds about 0.37 and the elbow has no sharp bend: the diagnostics say the groups are not there. A segment profile table translates each cluster back into original units with a plain-English label, which is the output a marketing or risk team actually uses.

k-means objective (within-cluster sum of squares)

min over C₁…C_k Σ_j Σ_{x ∈ C_j} ‖x − μ_j‖²

C_j is the set of customers in segment j, μ_j its centre (the mean of its members) and ‖x − μ_j‖ the Euclidean distance on the standardised features. Lloyd's algorithm alternates assignment (each customer to the nearest μ_j) and update (μ_j = mean of C_j); each step can only lower the objective, so it converges, but only to a local optimum, hence k-means++ seeding and several restarts.

Frequently asked questions

How do you choose the number of clusters k?

Plot within-cluster sum of squares against k and look for the elbow where extra clusters stop buying much, and check the average silhouette score, which is high when clusters are tight and well separated. Then apply business judgement: a segment that cannot be acted on, or is too small to matter, is not worth keeping. If no k gives a clear elbow or a silhouette above about 0.5, there may be no real segments at all.

Why do features need to be standardised before k-means?

Because k-means uses Euclidean distance, a feature measured in thousands of dollars dominates one measured in counts per year. Z-scoring each feature (subtract the mean, divide by the standard deviation) puts them on a comparable footing so the clusters reflect structure, not units.

What are the limitations of k-means?

It assumes roughly spherical clusters of similar size and spread, it needs k chosen in advance, it is sensitive to outliers and scaling, and it can converge to a local optimum. Practitioners use k-means++ seeding and several restarts, and switch to Gaussian mixtures or density-based methods for elongated or irregular groups.

Interactive explorer

The live model with sliders, charts and real-time statistics loads on this page. If it does not appear, enable JavaScript or open https://www.beyondaverages.org/segmentation in a modern browser.

Related modules

  • Dice & convolution: Interactive 3D dice simulator. Roll up to five dice and watch the empirical sum histogram converge to the exact convolution probabilities: uniform to triangular to bell-shaped, the central limit theorem made visible.
  • Cards & hypergeometric: Interactive card-dealing simulator. Deal hands from a 3D deck and compare empirical frequencies of aces, pairs and colors against exact hypergeometric probabilities, and see why a deck's memory matters.
  • Frequency: Interactive Poisson distribution explorer for insurance claim counts and hospital admissions. Move the rate λ and watch P(X=k), the mode, and the probability of zero or extreme counts update live.
  • Severity: Interactive loss severity explorer comparing Log-Normal, Pareto (heavy-tail) and Gamma distributions. Adjust parameters and see the mean, median, 95th and 99th percentile claim sizes, and the share of total cost from the largest 5% of claims, update in real time.