So far, every model in this track has learned from data with the answers attached. Now we remove the answers. K-Means looks at data with no labels at all and discovers natural groups on its own. Businesses use it every day to understand their customers.
- Level: Intermediate
- Time: about 35 minutes
- Needs: Modules 1, 2, 5 and 6
By the end of this module you will be able to
- Explain what unsupervised learning and clustering are
- Describe the four steps of the K-Means algorithm
- Choose the number of clusters using the elbow method and silhouette score
- Explain why scaling and starting positions matter
- Turn clusters into meaningful customer segments
- Know when K-Means fails, and what to use instead
- Segment customers with K-Means in scikit-learn
11.1Unsupervised learning and clustering
In Module 1 you met three types of machine learning. Modules 3 to 10 were all supervised: every training example came with the right answer (a price, "spam", "left"). Now we move to unsupervised learning, where there are no answers at all.
Without answers, we can't predict a label. Instead, we ask a different question: what natural groups exist in this data? Finding those groups is called clustering.
| Classification (supervised) | Clustering (unsupervised) | |
|---|---|---|
| Data | Comes with labels | No labels |
| Question | "Which known group does this belong to?" | "What groups exist?" |
| Groups | Decided in advance by people | Discovered by the algorithm |
| Example | Will this customer leave: yes or no? | What types of customer do we have? |
Sorting a bag of mixed sweets
Tip a bag of mixed sweets onto a table and nobody tells you the categories. You'd still naturally sort them: the chocolates here, the fruity chews there, the mints in another pile. You created groups based on how similar they are. That's clustering.
In this module, we'll help a UK online shop understand its customers. It knows what each customer spends and how often they order, but nobody has ever grouped them into "types". K-Means will find those types for us.
11.2How K-Means works
K-Means is the most popular clustering algorithm. The "K" is the number of groups you want, and "means" refers to averages: each group is represented by the average (centre) of its points, called a centroid.
| Step | What happens |
|---|---|
| 1. Choose k and place centroids | Decide how many clusters you want, and drop that many centroids onto the chart |
| 2. Assign | Every point joins the cluster of its nearest centroid (using distance, like KNN in Module 6) |
| 3. Update | Move each centroid to the average position of all the points in its cluster |
| 4. Repeat | Go back to step 2. Stop when points no longer change cluster |
That's the whole algorithm. It's simple, fast and works surprisingly well. Try it step by step:
Try it: run K-Means one step at a time
Try a few "new random starts" with k = 4. Sometimes the centroids find the four obvious groups; occasionally two centroids get stuck sharing one group. That leads to an important practical point in lesson 11.4.
11.3Choosing the number of clusters
K-Means needs you to choose k up front. But if nobody knows the groups, how do you know how many there are? There are two common tools.
The elbow method
Inertia measures how tightly packed the clusters are: the total squared distance from every point to its centroid. Smaller is tighter. But inertia always falls as k increases (with one cluster per point, it would be zero!), so we don't just pick the smallest. Instead, we look for the point where adding another cluster stops helping much: the elbow of the curve.
The silhouette score
The silhouette score asks, for every point: am I much closer to my own cluster than to the next nearest one? It runs from −1 to 1. Close to 1 means clear, well-separated clusters. Close to 0 means clusters overlap. Pick the k with the highest score.
Business sense matters too
The maths gives you a shortlist, but the final choice is often a business decision. A marketing team might happily use 4 segments, but could never run 15 different campaigns. Choose a k that is both supported by the data and useful in practice.
11.4Scaling and starting positions
Always scale your features
K-Means uses distances, just like KNN. If annual spend is in hundreds of pounds and orders per year is in single figures, spend will completely dominate, and the clusters will be based on spend alone. Scale your features first (Module 2).
Starting positions can change the result
As you may have seen in the lab, a bad starting position can leave K-Means stuck with a poor answer. scikit-learn protects you in two ways, and both are switched on by default:
| Setting | What it does |
|---|---|
init="k-means++" | Picks smart starting centroids that are spread out, instead of purely random ones |
n_init=10 | Runs the whole algorithm 10 times from different starts and keeps the best result (lowest inertia) |
random_state=42 | Makes the random starts the same every time, so your results can be repeated |
11.5From clusters to customer segments
K-Means only gives each customer a number: cluster 0, 1, 2 or 3. The numbers mean nothing on their own. The real value comes when you describe each cluster by looking at its averages, then give it a name a business can use.
Here are the four clusters found in the Python example, sorted by spend:
| Segment name | Customers | Average spend | Orders a year | Days since last order | Suggested action |
|---|---|---|---|---|---|
| Loyal regulars | 294 | £1,823 | 24 | 12 | Reward with a loyalty scheme; ask for reviews |
| Big spenders gone quiet | 288 | £921 | 3 | 204 | Win-back campaign: they're valuable but drifting away |
| Casual shoppers | 421 | £361 | 6 | 43 | Encourage larger baskets with bundle offers |
| Lapsed | 197 | £164 | 2 | 323 | Low-cost reminder emails, or let them go |
Now a marketing team has four clear groups, each with a different plan. That's the kind of insight that makes clustering so valuable in data analyst and BI roles.
Clusters aren't "truth"
K-Means will always find k groups, even in data that has no real groups at all. Always check that the clusters make sense, are stable when you rerun them, and are actually useful. A cluster is a helpful summary, not a fact about the world.
11.6When K-Means fails
K-Means assumes clusters are roughly round blobs of similar size. When groups have other shapes, it struggles, because it always splits the space with straight boundaries between centroids.
| Algorithm | How it groups points | Good for |
|---|---|---|
| K-Means | Nearest centroid | Round, similar-sized groups; large datasets; fast results |
| DBSCAN | Areas where points are densely packed | Odd shapes; finding outliers (it labels lonely points as "noise"); no need to choose k |
| Hierarchical clustering | Merges the closest points and groups step by step, building a family tree (dendrogram) | Smaller datasets; seeing groups at several levels of detail |
| Gaussian mixture models | Each cluster is a stretched bell-shaped cloud; points get a probability of belonging to each | Oval-shaped or overlapping groups |
Other weaknesses of K-Means: outliers can drag a centroid away from its group, and you must choose k in advance.
11.7Customer segmentation in Python
We'll create 1,200 customers with three features: annual spend, orders per year and days since their last order. (These are close to the "RFM" features, recency, frequency and monetary value, that retailers really use.) We hide four groups in the data, then see if K-Means can find them without being told.
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score
# 1. 1,200 customers of a UK online shop (no labels!)
rng = np.random.default_rng(11)
groups = [ # (how many, spend £, orders per year, days since last order)
(300, 1800, 24, 12), # these hidden groups are what we hope to find
(400, 350, 6, 40),
(300, 900, 3, 200),
(200, 150, 2, 320),
]
rows = []
for size, spend, orders, days in groups:
rows.append(pd.DataFrame({
"annual_spend": rng.normal(spend, spend * 0.25, size).clip(20),
"orders": rng.normal(orders, orders * 0.3, size).clip(1).round(),
"days_since": rng.normal(days, days * 0.25, size).clip(1).round(),
}))
df = pd.concat(rows, ignore_index=True)
# 2. Scale: spend is in hundreds, orders in single figures
X = StandardScaler().fit_transform(df)
# 3. Try k = 2 to 7 and compare inertia and silhouette
for k in range(2, 8):
km = KMeans(n_clusters=k, n_init=10, random_state=42).fit(X)
sil = silhouette_score(X, km.labels_)
print(f"k={k} inertia={km.inertia_:7.0f} silhouette={sil:.3f}")
# 4. Fit the chosen model and describe each cluster
km = KMeans(n_clusters=4, n_init=10, random_state=42).fit(X)
df["cluster"] = km.labels_
profile = df.groupby("cluster").agg(
customers=("annual_spend", "size"),
avg_spend=("annual_spend", "mean"),
avg_orders=("orders", "mean"),
avg_days_since=("days_since", "mean"),
).round(0).sort_values("avg_spend", ascending=False)
print(profile)
k=2 inertia= 1441 silhouette=0.598
k=3 inertia= 710 silhouette=0.579
k=4 inertia= 459 silhouette=0.623
k=5 inertia= 356 silhouette=0.566
k=6 inertia= 303 silhouette=0.550
k=7 inertia= 258 silhouette=0.548
customers avg_spend avg_orders avg_days_since
cluster
2 294 1823.0 24.0 12.0
1 288 921.0 3.0 204.0
0 421 361.0 6.0 43.0
3 197 164.0 2.0 323.0What the output tells us
- Inertia drops sharply from k = 2 to 4 (1,441 → 459), then only slowly. That's the elbow.
- The silhouette score peaks at k = 4 (0.623). Both tools point to four groups.
- The cluster profiles almost exactly match the four hidden groups we built into the data (for example, the top cluster averages £1,823 spend, 24 orders and 12 days, against the hidden values of £1,800, 24 and 12). K-Means found them with no labels at all.
- The cluster numbers (0 to 3) are arbitrary. Run it with a different
random_stateand the groups might be numbered differently, which is why we sort and name them ourselves.
Using the clusters next
Clusters are often used as a starting point for other work: a feature in a supervised model, a column in a Power BI dashboard, or the audience list for a marketing campaign. Use km.predict() to assign new customers to the existing segments.
11.8Strengths, weaknesses and real-world uses
| Strengths | Weaknesses |
|---|---|
| Simple to understand and explain | You must choose k |
| Fast, even on millions of rows | Assumes round, similar-sized clusters |
| No labels needed | Sensitive to scaling and outliers |
| Easy to apply to new data: just find the nearest centroid | Always finds k groups, even if there are no real ones |
| Industry | How clustering is used |
|---|---|
| Retail and marketing | Customer segmentation for targeted offers and campaigns |
| Local government | Grouping areas by population, income and service use to plan services |
| Streaming and media | Grouping viewers by taste to improve recommendations |
| Logistics | Finding the best places for warehouses or delivery hubs, near clusters of customers |
| Images | Reducing an image to a few main colours (colour quantisation) |
| Security | Spotting unusual behaviour that doesn't fit any normal cluster |
SummaryKey takeaways
- Unsupervised learning works with no labels; clustering finds natural groups.
- K-Means repeats two steps: assign each point to its nearest centroid, then move each centroid to the average of its points.
- Choose k with the elbow method (inertia) and the silhouette score, plus business sense.
- Always scale features; use k-means++ and n_init to avoid bad starting positions.
- Turn clusters into value by profiling and naming them as segments.
- K-Means assumes round, similar-sized clusters; try DBSCAN or hierarchical clustering for other shapes.
- K-Means always finds k groups, so check they are real and useful.
Check your understanding
Your machine learning roadmap
- 01Introduction to Machine LearningWhat ML is and how it works
- 02Preparing Data for Machine LearningFeatures, encoding, scaling, train/test split
- 03Linear RegressionPredicting numbers
- 04Logistic RegressionPredicting yes or no
- 05Evaluating ModelsAccuracy, precision, recall, overfitting, cross-validation
- 06K-Nearest NeighboursLearning from similar examples
- 07Decision TreesFlowcharts that learn
- 08Random Forest and Gradient BoostingMany models working together
- 09Support Vector MachinesFinding the best boundary
- 10Naive BayesProbability-based classification
- 11Clustering with K-MeansYou are here
- 12Dimensionality Reduction with PCASimplifying big datasets
- 13Capstone ProjectBuild and present a full ML project
Next module
Dimensionality Reduction with PCA