Day 78 ยท Sorting a garage sale, unlabeled

Clustering

You will be able to
  • Explain the k-means assign/update loop and why it always converges (to something)
  • Choose k using the elbow and silhouette methods, and say why neither is an oracle
  • Explain when DBSCAN beats k-means and what its two parameters control
  • Profile clusters back in original units and give each a name a stakeholder would recognize
  • State why scaling is mandatory for distance-based methods
Today's ~120 minutes
Spaced-rep warm-up: due cards from Week 11 + Day 50 distance card10 min
ELI5 + tech read; k-means visualizer (watch centroids walk)20 min
Guided: k-means on checkable customers + profiling37 min
Practice: break k-means, call DBSCAN20 min
Project: segmentation mini-report23 min
Quiz + flashcards10 min

Builds on: Day 50 โ€” Vectors, norms & distance ยท Day 69 โ€” Scaling & why it matters ยท Day 66 โ€” pandas groupby for profiling

The analogy

You inherit a garage full of a stranger's stuff โ€” no labels, no inventory. So you start making piles: this feels kitchen-ish, that pile is clearly tools, those boxes are holiday decorations. Nobody told you the categories; you invented them by putting similar things near each other and noticing where the gaps fall. Two friends sorting the same garage would produce slightly different piles โ€” and both could be defensible. That is clustering: discovering structure with no answer key.

k-means is a specific sorting strategy: guess k pile-locations, assign every item to its nearest pile, move each pile-marker to the middle of what it collected, and repeat until nothing moves. It is fast and usually sensible, but it has a personality: it wants round piles of similar size, and it will force EVERY item into some pile โ€” the broken umbrella gets jammed into "sporting goods" because k-means has no concept of "this belongs nowhere." DBSCAN is the other personality: it grows piles wherever items are densely packed and honestly labels the stragglers as noise. Today you learn both, and โ€” the part that makes it useful โ€” how to walk back to the piles and name them.

Why this matters on the job

Clustering is the first unsupervised tool customers actually buy: "segment our customers," "group these support tickets," "find anomalous transactions" are recurring FDE briefs, and all are clustering jobs. It is also the analysis layer of modern LLM work: on Day 143 you will cluster embedded production failures to find what KIND of errors dominate โ€” same algorithm, embedding vectors instead of spending columns. And the interview classic "how do you choose k, and what are k-means' failure modes?" is answerable only if you have watched it fail on your own screen, which you will today.

Watch it happen

Sorting the garage sale โ€” k-means assigns, then moves the centers

step 1 / 5
$12
0
$15
1
$18
2
$22
3
$71
4
$75
5
$82
6
$90
7

Eight customers described by one number each (monthly spend). No labels โ€” we want k=2 groups to emerge on their own.

Guided practice

guided 1

k-means on customers you can check

22 min
  1. Paste the starter. It synthesizes 600 customers from four KNOWN behavioral groups (so you can check the sorting against truth โ€” a luxury real projects never have), scales them, and runs k-means for k = 2โ€ฆ8 printing inertia and silhouette.
  2. Run it. Find the elbow in the inertia column and the peak in the silhouette column. Do they agree on k = 4?
  3. Fit the final k=4 model and cross-tabulate found labels vs true groups (pd.crosstab). Expect near-diagonal โ€” but note any confusion and look at WHICH two true groups blur.
  4. Rerun the whole thing WITHOUT the scaler. Watch silhouette drop and the crosstab smear โ€” write one sentence on which feature's units took over.
  5. Note cluster label arbitrariness: refit with random_state=1 and watch the same piles get different numbers. Labels are names, not order.
๐Ÿ python โ€” editable, runs in your browser
Ctrl/โŒ˜+Enter runs ยท Tab indents ยท numpy/pandas/sklearn auto-load on import (torch and network calls need a local run)
guided 2

Profile and name the piles

15 min
  1. Build a DataFrame of the ORIGINAL unscaled features plus the k=4 labels: columns spend, visits, tenure, cluster.
  2. df.groupby("cluster").agg(["mean", "count"]) โ€” read each cluster's profile against the overall means (df.mean()).
  3. Write one plain-English name and one sentence per cluster, as if labeling a slide for a marketing VP. Compare your names to the true group names in the generator โ€” did the data support the story?
  4. The habit to keep: profiling happens in ORIGINAL units. Nobody budgets in standard deviations.
๐Ÿ python โ€” editable, runs in your browser
Ctrl/โŒ˜+Enter runs ยท Tab indents ยท numpy/pandas/sklearn auto-load on import (torch and network calls need a local run)

On your own

Break k-means, call DBSCAN

20 min

Generate two datasets k-means cannot handle: make_moons(n_samples=400, noise=0.07, random_state=0) and make_blobs(n_samples=[300, 40], centers=[[0, 0], [4, 4]], cluster_std=[1.6, 0.4], random_state=0) (one diffuse blob, one tight one).

Goal: (1) run KMeans(n_clusters=2) on each and describe how it fails (check silhouette AND eyeball a scatter plot or the crosstab if truth is available); (2) run DBSCAN on the moons (start eps=0.2, min_samples=5) and confirm it recovers both crescents plus a few โˆ’1 noise points; (3) tabulate DBSCAN's cluster count and noise count for eps in [0.1, 0.2, 0.3, 0.5] โ€” write two sentences on eps sensitivity; (4) one-sentence verdict: for each dataset, which algorithm would you ship?

Hints: k-means slices the moons with a straight frontier because centroid-nearest is a linear boundary between two centroids; DBSCAN follows density. The mixed-density blobs are DBSCAN's OWN weakness โ€” one eps cannot fit both densities.

Ship before you stop

Segmentation mini-report

Create segments_lab.py + SEGMENTS.md in your practice repo. The script: generates the guided customers (or loads any tabular dataset you prefer), scales, selects k with both elbow and silhouette evidence (printed table), fits final k-means, and emits a per-cluster profile table in original units with population comparison. The report: your chosen k with a two-line justification citing both signals, a named one-sentence profile per cluster, one action a business could take per segment, and a limitations paragraph naming two k-means assumptions your data may violate. This artifact structure โ€” evidence, profile, action, limitations โ€” is exactly the Day 143 failure-clustering deliverable with embeddings swapped in. Commit both files.

Rubric โ€” check what you completed (0/5)

Common mistakes & misconceptions

  • Clustering unscaled features. Distance-based methods weight features by their units; the biggest-numbered column silently becomes the only feature. Scale first, every time.
  • Reading cluster labels as ordered or stable. Label 0 vs 2 is arbitrary naming that changes with random_state; only the groupings mean anything.
  • Minimizing inertia to choose k. Inertia monotonically decreases with k โ€” at k = n it hits zero. Use the elbow's bend or silhouette's peak, then apply domain judgment.
  • Forgetting k-means assigns EVERYTHING. Outliers get jammed into the nearest pile and drag its centroid; DBSCAN's โˆ’1 label, or outlier pruning first, handles them honestly.
  • Expecting k-means to find crescents, rings, or elongated groups. Nearest-centroid partitions are convex regions; non-globular shapes need DBSCAN, spectral, or better features.
  • Shipping clusters without profiling them. An unlabeled integer per row is not a segmentation โ€” the names, the evidence, and the recommended action are the deliverable.
Knowledge check

Q1. You cluster customers on unscaled income (20kโ€“200k) and age (18โ€“80). What happens?

Q2. Why can't you choose k by picking the k with the lowest inertia?

Q3. Two crescent-moon shaped classes. k-means with k=2 fails but DBSCAN succeeds becauseโ€ฆ

Go deeper โ€” curated resources

docsscikit-learn User Guide โ€” 2.3 Clustering โ†—30 minvideoStatQuest โ€” k-means clustering, clearly explained โ†—15 mincourseGoogle ML Crash Course โ€” clustering module โ†—20 min
If you have a third hour
  • HDBSCAN โ€” density clustering that handles varying density โ€” The mixed-density blobs that broke DBSCAN in practice are HDBSCAN's home turf: it effectively varies eps per region. sklearn ships it as sklearn.cluster.HDBSCAN โ€” rerun your practice data through it.
Done means
  • k selected with elbow + silhouette agreement; crosstab vs truth examined
  • Unscaled rerun performed and the dominating feature named
  • DBSCAN recovers the moons; eps-sensitivity table written
  • Segmentation report with named, actionable clusters committed
  • Quiz โ‰ฅ 2/3
How this connects

โ† Back: Every distance today is Day 50's Euclidean norm, and the scaling mandate is Day 69's lesson applied to an algorithm that is NOTHING BUT distances. Cluster profiling is Day 66's groupby earning its keep.

Forward โ†’: Tomorrow PCA compresses dimensions so clusters become visible โ€” the standard pairing. On Day 92 embeddings turn text into vectors, and on Day 143 you will cluster embedded production failures with today's exact workflow: scale, cluster, profile, name.

Unlocks: D79 PCA & Dimensionality