Stop 09 · Advanced Analytics & Predictive BasicsTopics · 5/8

Clustering Basics (K-Means Overview)

Part of the Data Analyst Roadmap

In short

Marketing wants 'customer segments' but nobody knows what they are yet. Clustering finds natural groups in the data without you telling it the answer first.

An unsupervised method for grouping similar data points together without predefined labels — commonly used for customer segmentation.

The jaadu sorts a heap of socks into piles by colour.

Think of it like this

Sorting the laundry pile

After a hostel laundry day, there's one big heap of socks. Nobody labelled them, but you naturally make piles: blues, greys, reds. You keep adjusting until each pile looks right. K-means does this with customers: pick a number of piles, then shuffle until they settle.

Watch it happen

K-means with k = 3 on monthly spend

200
0
250
1
300
2
1200
3
1250
4
1300
5
4800
6
5000
7
5200
8

1/5Nine customers' monthly spend in rupees. We ask for 3 clusters.

The big ideas

What you need to know

You choose k

K-means needs the number of clusters up front. Try a few and use the elbow method plus common sense.

Scale features first

Spend in thousands and visits in single digits? Standardise, or spend alone decides everything.

You name the clusters

The algorithm gives numbers. Profiling each group's averages is how you turn them into personas.

Starting points matter

Different random starts can give different clusters. Use n_init and a fixed random_state.

See it in code

Walk through the code

python
1from sklearn.preprocessing import StandardScaler2from sklearn.cluster import KMeans3 4features = rfm[["recency_days", "orders", "spend"]]5X = StandardScaler().fit_transform(features)6 7for k in range(2, 8):8    print(k, KMeans(n_clusters=k, n_init=10, random_state=42).fit(X).inertia_)9 10km = KMeans(n_clusters=4, n_init=10, random_state=42).fit(X)11rfm["segment"] = km.labels_12print(rfm.groupby("segment")[["recency_days", "orders", "spend"]].mean().round(1))

1/4Recency, frequency and money, scaled so each counts equally.

Classic RFM segmentation: scale, check a few values of k, fit, then profile each segment by its averages.

Save yourself the pain

Common mistakes

Oops

Feeding raw, unscaled columns into K-means.

Do this instead

Standardise features first so a column with big numbers doesn't dominate the distances.

Oops

Presenting 'Cluster 0, 1, 2' to the business.

Do this instead

Profile each cluster and give it a meaningful name with its size and key averages.

Your mission

Segment some customers

~35 min

Quick check

Why scale features before running K-means?

Go deeper

  1. 1Read a conceptual (not heavily mathematical) explanation of how K-Means works.
  2. 2Run a basic K-Means clustering example on a sample customer dataset.
  3. 3Learn why choosing the right number of clusters (K) is more art than science.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge