Machine learning models · Phase 3 · Lesson 19

k-means clustering

Use k-means to partition numeric data and understand its geometric assumptions.

This is one focused step in the 60-lesson course. Use the retrieval check before moving on.

Lesson goal

Use k-means to partition numeric data and understand its geometric assumptions.

The core idea

Place k centres, assign each point to its nearest centre, move centres to the assigned averages, and repeat.

Mental model

Picture it this way. Place k centres, assign each point to its nearest centre, move centres to the assigned averages, and repeat. The important question is what assumption this picture makes, and whether that assumption fits the data.

Mathematical core

K-means minimises the sum of squared distances from each point to its assigned centroid. It prefers compact, roughly spherical clusters under the chosen scaling.

Worked example

Group stores by average basket size and visit frequency. If one variable has a much larger unit, it can decide the clusters unless the features are scaled.

When to use it

It earns a place when

  • Use it for a fast exploratory baseline when a distance and a plausible number of compact groups exist.
  • You can evaluate it against a credible baseline.
  • Its output fits the decision and data constraints.

Do not make it the default when

  • Do not assume the clusters are real customer types just because an algorithm returns labels, and do not use raw categorical data with Euclidean distance.
  • A simpler model has not been tested.
  • The data or target definition is still unclear.

Failure modes

Watch for this. The result depends on initial centres, k and scaling. Non-spherical or uneven-density groups can be split badly.

When a result looks surprisingly good, inspect the split, target timing, error slices and data-generating process before celebrating.

Practice

Use this as a small experiment rather than a recipe to copy blindly. Change one thing, record the result and explain the change.

Do this. Scale a two-dimensional dataset, run k-means for several k values, plot inertia and inspect whether the groups make domain sense.

Retrieval check

Answer from memory first. The buttons reveal feedback, but the durable step is explaining why.

1. What does k-means optimise?

2. Why can k-means scaling change the answer?

3. What does choosing k require?

Transfer prompt. Describe one real problem where this model or idea would be a sensible candidate. Name the target, the main risk and the metric you would inspect.

Primary source

scikit-learn User Guide. Use the source for the deeper treatment after you can explain the lesson's core idea without looking.