# Clustering for customer segmentation

> Customer segmentation from the data: letting cluster analysis suggest the segments instead of guessing them.

- Course: [Statistics for Product Managers and Marketers](https://productovi.com/courses/statistics-for-pm-and-marketers.md)
- Module: Modelling and segmentation
- Lesson 22/24, ~39 min
- Source: https://productovi.com/courses/statistics-for-pm-and-marketers/clustering

## Cluster analysis: grouping without labels

**Alex Tutor:** Last module, and you're still on CatChow's product team. Marketing has dropped a new request on your desk: customer segmentation.

**Sofia:** Marketing wants 'customer segments' for CatChow. I was just going to guess three: cheapskates, regulars, whales.

**Alex Tutor:** You can guess, or you can let the data tell you. That family of methods is called *cluster analysis* — finding groups of similar customers without predefined labels.

**Sofia:** Isn't that the classification thing from logistic regression?

**Alex Tutor:** Close but backwards. Classification sorts objects into *known* classes you already named. Clustering finds the groups themselves — you don't even know how many exist yet.

![A cat sorts a heap of mixed toys into three piles of similar items, balls, toy mice and feathers, without any bins or labels.](https://productovi.com/courses/statistics-for-pm-and-marketers/inline/clustering-illustration-2.webp)

*Clustering doesn't start from named groups: it lets similar things gather and the groups appear on their own.*

**Alex Tutor:** Make sure the difference sticks. Which tool fits this request?

> **Exercise:** Marketing has last year's customers already labelled "churned" or "active" and wants to predict the label for new customers. Which approach fits?

**Alex Tutor:** Two situations come up. Either you know how many groups to expect, or you have no idea. The method you pick depends on which one you're in.

**Sofia:** I have no idea how many segments CatChow customers actually split into.

> **Exercise:** You have no idea how many customer segments CatChow actually has. Which family of methods fits this situation?

## Measuring how similar two customers are

**Alex Tutor:** Every clustering method starts the same way: turn 'how similar are these two customers' into a number — a *distance*.

**Sofia:** Distance like kilometres between their houses?

**Alex Tutor:** Distance in the space of their metrics. Say customer A spends 100 UAH/month with 1 order, B spends 120 UAH with 1 order. They're close in that space.

$$
d_{\text{Euclidean}} = \sqrt{\sum_i (x_i - y_i)^2}
$$

*Euclidean distance: the straight-line distance across all variables.*

**Alex Tutor:** That's the *Euclidean* distance — the ordinary straight-line one. There's also *Manhattan* distance, which sums absolute differences instead of squaring them.

**Sofia:** Named after the borough? Why would statisticians care about Manhattan traffic?

**Alex Tutor:** Because you can't cut diagonally through city blocks — you walk along the grid. It's less thrown off by one huge outlier variable than Euclidean distance is.

![A city grid from above: a cat walks an L-shaped route along the streets, while a bird flies a straight diagonal between the same two corners.](https://productovi.com/courses/statistics-for-pm-and-marketers/inline/clustering-illustration-3.webp)

*The cat's route is Manhattan distance; the bird's straight line is Euclidean distance.*

> **Exercise:** Customer A spends 100 UAH/month with 1 order; customer B spends 120 UAH with 1 order. What is the Euclidean distance between A and B on (spend, orders)?

## Hierarchical clustering

**Alex Tutor:** When you don't know the number of groups, use *hierarchical clustering*. It merges the two most similar customers, recomputes distances, then merges again.

**Sofia:** It keeps merging... forever?

**Alex Tutor:** Until every customer is in one giant cluster. Along the way it draws a *dendrogram* — a tree you can cut at any height to get however many groups you want.

**Sofia:** So the tree decides the number of clusters for me, after the fact?

**Alex Tutor:** You decide, by choosing where to cut — usually where branches are tall, meaning the merged groups were quite different from each other.

![An upside-down tree whose branches merge into one trunk, with cat-head tips at the bottom and orange scissors cutting across the tall branches.](https://productovi.com/courses/statistics-for-pm-and-marketers/inline/clustering-illustration-4.webp)

*Where you cut the tree decides how many groups you get; tall branches are the natural place to cut.*

> **Exercise:** What does hierarchical clustering produce that lets you choose the number of groups after the fact?

## k-means clustering, step by step

**Alex Tutor:** Now say you *do* know the number: leadership wants exactly 2 spend-based segments. Then the right tool is *k-means*.

**Alex Tutor:** Take four customers by (monthly spend, orders/month): A(100, 1), B(120, 1), C(500, 5), D(520, 5). k-means places 2 *centroids* — group representatives — and moves them until they settle.

**Four customers, two spend clusters**

| Monthly spend, UAH | Small spenders (A, B) | Big spenders (C, D) |
| --- | --- | --- |
| 100 | 1 |  |
| 120 | 1 |  |
| 500 |  | 5 |
| 520 |  | 5 |

*A and B (small spenders) sit far from C and D (big spenders); k-means groups them exactly along that gap.*

**Sofia:** So the centroid of {C, D} is just their average spend and average order count?

**Alex Tutor:** Exactly: (500 + 520) / 2 = 510 UAH, and (5 + 5) / 2 = 5 orders. That point becomes the new centroid for that cluster.

**Sofia:** And every customer just checks which centroid is closer and jumps there if needed?

**Alex Tutor:** Right — assign to nearest centroid, recompute centroids as the mean of their members, repeat, until nobody moves anymore.

> **Exercise:** Customers C and D, described by (monthly spend in UAH, orders per month), sit at C(500, 5) and D(520, 5), and k-means puts them in one cluster. What is the centroid's average monthly spend for this cluster?

**Sofia:** So after the centroids move, do we just... stop?

**Alex Tutor:** Not yet. With new centroid positions, some customers may now be closer to a different one, so we reassign and recompute again.

**Sofia:** So it loops until nothing changes anymore?

**Alex Tutor:** Exactly — that's convergence. Try ordering the algorithm's steps yourself before we move on.

> **Exercise:** Put the steps of the k-means algorithm in the right order.

## Before you hit run: scaling and choosing k

**Alex Tutor:** One trap: spend is measured in hundreds of hryvnias, orders in single digits. Distances would be dominated entirely by spend.

**Alex Tutor:** See it for yourself with A and C. C spends 400 UAH more and orders 4 times more often. How far apart are they?

> **Exercise:** Customer A is at (spend, orders) = (100 UAH, 1) and customer C at (500 UAH, 5). What is the Euclidean distance between them on the raw values?

> **Standardise before clustering**
>
> Convert every variable to a z-score first. Otherwise a variable measured on a large scale silently drowns out the others in every distance calculation.

**Sofia:** So orders/month would basically be ignored otherwise?

**Alex Tutor:** Almost. And one more thing: k-means depends on where centroids start. Run it several times — if you get different groups each time, your chosen k may be wrong.

**Sofia:** How do I even pick k in the first place?

**Alex Tutor:** Business judgement first — how many segments can marketing actually act on? Then check how much clustering error drops as k grows, and stop once it flattens out.

> **Exercise:** Because k-means uses distances, a variable measured in hundreds of hryvnias can dominate the clustering unless you standardise all variables first.

## Wrapping up

**Alex Tutor:** So: hierarchical when you don't know k, k-means when you do, and always standardise your variables first.

**Sofia:** So my 'cheapskates, regulars, whales' guess might not survive contact with real distances?

**Alex Tutor:** It might match perfectly, or the data might split on something you never guessed — frequency instead of spend, say. Let's write that up.

**Sofia:** Before I run anything — remind me what to check first?

> **Exercise:** Which of these are good practices before running k-means on CatChow customer data? Select all that apply.

**Alex Tutor:** Before we get to a messy real scenario, put the units problem in your own words first.

**Sofia:** Uh oh, the dreaded free-response box.

**Alex Tutor:** You'll survive. Here's the setup.

**Sofia:** Fire away.

> **Exercise:** In 2–3 sentences, explain why four CatChow customers, A(100 UAH, 1 order), B(120, 1), C(500, 5) and D(520, 5), could end up in different clusters depending on whether spend is measured in UAH or in thousands of UAH.

**Alex Tutor:** Good instinct. Now a messier, real situation — someone already ran a clustering and the results look off.

**Sofia:** Let me guess: too many segments?

**Alex Tutor:** You've clearly been paying attention.

**Sofia:** Show me the mess.

> **Exercise:** Write your recommendation for the growth marketer.

**Alex Tutor:** That's clustering for customer segmentation. Next: factor analysis, for when it's not customers you want to group, but survey questions.

- Previous lesson: [Models and forecasting](https://productovi.com/courses/statistics-for-pm-and-marketers/models-and-forecasts.md)
- Next lesson: [Factor analysis: surveys and NPS drivers](https://productovi.com/courses/statistics-for-pm-and-marketers/factor-analysis.md)

---
Productovi Learning. Free, no sign-up needed to read.
