CadetX
CX Learn | Complete Machine Learning Guide
Scikit-Learn · Python
CadetX logo CadetX CX Learn
ML-101 · Module 11 Intermediate about 35 minutes 9 Lessons Prereq: Modules 1, 2, 5 and 6

K-Means Clustering

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)
DataComes with labelsNo labels
Question"Which known group does this belong to?""What groups exist?"
GroupsDecided in advance by peopleDiscovered by the algorithm
ExampleWill 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.

K-Means in four stages. First, four centroids are placed. Second, each point joins its nearest centroid. Third, each centroid moves to the middle of its points. Finally, after repeating, the clusters settle.1. Place 4 centroids2. Assign to nearest3. Move centroids4. Repeat until stable
Figure 1. K-Means on customer data, from a deliberately poor starting position. The crosses are the centroids. They start in the wrong places, but after a few rounds of "assign, then move", they settle in the middle of the four real groups.
StepWhat happens
1. Choose k and place centroidsDecide how many clusters you want, and drop that many centroids onto the chart
2. AssignEvery point joins the cluster of its nearest centroid (using distance, like KNN in Module 6)
3. UpdateMove each centroid to the average position of all the points in its cluster
4. RepeatGo 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.

Left: inertia falls sharply from k equals 2 to 4, then flattens, making an elbow at 4. Right: silhouette score is highest at k equals 4.Elbow method: inertia050010001500234567Elbow at k = 4Number of clusters (k)Silhouette score0.500.550.600.65234567Number of clusters (k)
Figure 2. Real results from the Python example in lesson 11.7. Inertia drops steeply up to k = 4, then flattens: that's the elbow. The silhouette score is also highest at k = 4. Both methods agree.

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:

SettingWhat it does
init="k-means++"Picks smart starting centroids that are spread out, instead of purely random ones
n_init=10Runs the whole algorithm 10 times from different starts and keeps the best result (lowest inertia)
random_state=42Makes 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 nameCustomersAverage spendOrders a yearDays since last orderSuggested action
Loyal regulars294£1,8232412Reward with a loyalty scheme; ask for reviews
Big spenders gone quiet288£9213204Win-back campaign: they're valuable but drifting away
Casual shoppers421£361643Encourage larger baskets with bundle offers
Lapsed197£1642323Low-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.

Two moon-shaped groups. K-Means cuts them with a straight line, mixing the moons. DBSCAN follows the shapes and finds each moon correctly.K-Means (k = 2)Cuts straight through the moonsDBSCANFollows each shape correctly
Figure 3. Two curved, moon-shaped groups. K-Means (left) cuts straight across them, mixing half of each moon together. DBSCAN (right), which groups points by how densely packed they are, follows each shape correctly.
AlgorithmHow it groups pointsGood for
K-MeansNearest centroidRound, similar-sized groups; large datasets; fast results
DBSCANAreas where points are densely packedOdd shapes; finding outliers (it labels lonely points as "noise"); no need to choose k
Hierarchical clusteringMerges the closest points and groups step by step, building a family tree (dendrogram)Smaller datasets; seeing groups at several levels of detail
Gaussian mixture modelsEach cluster is a stretched bell-shaped cloud; points get a probability of belonging to eachOval-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.

customer_segments.py
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)
Output
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.0

What 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_state and 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

StrengthsWeaknesses
Simple to understand and explainYou must choose k
Fast, even on millions of rowsAssumes round, similar-sized clusters
No labels neededSensitive to scaling and outliers
Easy to apply to new data: just find the nearest centroidAlways finds k groups, even if there are no real ones
IndustryHow clustering is used
Retail and marketingCustomer segmentation for targeted offers and campaigns
Local governmentGrouping areas by population, income and service use to plan services
Streaming and mediaGrouping viewers by taste to improve recommendations
LogisticsFinding the best places for warehouses or delivery hubs, near clusters of customers
ImagesReducing an image to a few main colours (colour quantisation)
SecuritySpotting 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

  1. 01
    Introduction to Machine LearningWhat ML is and how it works
  2. 02
    Preparing Data for Machine LearningFeatures, encoding, scaling, train/test split
  3. 03
    Linear RegressionPredicting numbers
  4. 04
    Logistic RegressionPredicting yes or no
  5. 05
    Evaluating ModelsAccuracy, precision, recall, overfitting, cross-validation
  6. 06
    K-Nearest NeighboursLearning from similar examples
  7. 07
    Decision TreesFlowcharts that learn
  8. 08
    Random Forest and Gradient BoostingMany models working together
  9. 09
    Support Vector MachinesFinding the best boundary
  10. 10
    Naive BayesProbability-based classification
  11. 11
    Clustering with K-MeansYou are here
  12. 12
    Dimensionality Reduction with PCASimplifying big datasets
  13. 13
    Capstone ProjectBuild and present a full ML project