learn-unsupervised-with-phoebe / Session 2 of 10
Learn Unsupervised with Phoebe · Session 2 of 10

What 'similar' means

Session 1 ended with a warning about a harmless-looking cents column. Today it detonates. Every clustering algorithm is a distance calculator wearing a trench coat - and whoever controls the distance controls the segments. You will watch raw units hand the entire Mango Lane analysis to the billing system's unit choice, then fix it with one preprocessing line and watch the five planted segments snap back into focus.

🟢 Easiest Builder track The lab opens today 45 min + self-study depth
0-3 · Welcome 3-20 · Concepts 20-40 · The scaling lab 40-45 · Q&A
Part 0

Why this session exists

Strip the branding off k-means, hierarchical clustering, DBSCAN - underneath, each one is asking the same question thousands of times: "how far apart are these two customers?" Every clustering algorithm is a distance calculator wearing a trench coat. That means the real decisions get made before the algorithm runs: which features describe a customer, and what units they arrive in. Whoever controls the distance controls the segments - and right now, on our data, the billing system controls the distance.

Live - covered in session Self-study - read after ✦ Lab moment 🥭 The Mango Lane case
★ What you walk out with today The ability to hand-compute why one feature is silently owning a distance calculation, the one-line fix (z-scaling) and exactly what it does, and a before/after you ran yourself: the same algorithm on the same customers scoring truth match 70% raw and 100% scaled. One preprocessing line beat any algorithm choice - you will say that sentence out loud by minute 40.
Part 1 · covers similarity design

Distance is the whole game 7 min live

A Mango Lane customer is three numbers - R, F, M - which makes her a point in 3D space. Once customers are points, "similar" gets a working definition: similar = nearby. Euclidean distance is the straight-line ruler between two points, built from the per-feature differences. But notice what just happened: "similar = nearby" is a CHOICE we made, not a fact about customers. The algorithm will enforce that choice ruthlessly.

Two customers as points (2 of the 3 RFM axes shown) Recency (days) Frequency A · R 20, F 4 B · R 40, F 12 ΔR = 20 ΔF = 8 distance How the number is built 1 · difference per feature ΔR = 20 · ΔF = 8 2 · square each difference 400 + 64 = 464 3 · add, take the square root √464 ≈ 21.5 Step 2 squares every difference - the feature with the biggest raw numbers owns the total.
🔍 Click to zoom - Euclidean distance: per-feature differences, squared, summed, rooted
LiveEuclidean distance in plain terms4 min▶

The straight-line ruler between two points, and the default distance inside k-means. For customers A and B it is computed per feature:

Difference each feature - how far apart are A and B on recency? On frequency? On monetary?

Square each difference - this makes everything positive and punishes big gaps more than small ones.

Add them up and take the square root - one number: how far apart these two customers are, in feature space.

That is the entire similarity engine of this course. Every cluster boundary you will ever see is downstream of this little recipe - which is why the squaring step deserves your suspicion. A difference of 20 becomes 400; a difference of 2,300 becomes 5,290,000. Big-unit features do not just win, they win squared.

Live'Similar = nearby' is a choice, not a fact3 min▶

Nothing in nature says two customers 21.5 units apart are "somewhat similar". WE decided that similarity means geometric closeness, on THESE three features, in THESE units. Change any of those decisions and different customers become neighbors - and different segments come out the other end. The algorithm never questions the ruler it is handed; it just measures, thousands of times, and reports the groups the ruler implies.

Real worldGoogle's ML clustering course spends more of its time on similarity design - how to define and weigh "similar" - than on the clustering algorithm itself. That allocation is the lesson: professionals treat the distance measure as the design surface and the algorithm as a commodity. Amateurs do the reverse, tuning algorithm knobs on top of a broken ruler.
Self-studyWhy we can trust one ruler for this course2 min▶

There are other rulers (Manhattan distance, cosine similarity, and more), each encoding a different opinion about what "alike" means. This course stays with Euclidean distance because it is what k-means optimizes and what our RFM features suit once scaled - but the deeper lesson travels: whichever ruler you pick, the units question of Part 2 ambushes all of them. Fix the units first; debate rulers later, if ever.

Part 2 · covers the planted trap

The cents disaster 🥭 7 min live

Look at the raw ranges in our export: recency runs 1-350 (days), frequency runs 0.2-25 (orders per year), monetary runs 4,500-12,000 - because billing exports speak CENTS. Now remember the squaring step. The cents column contributes MILLIONS to every squared distance while frequency contributes single digits. On raw units, distance IS money - everything else is decoration.

The three raw ranges, drawn to the same scale Recency 1-350 days Frequency 0.2-25 orders/yr (barely a sliver at this scale) Monetary 4,500-12,000 · CENTS, straight from the billing export distance lives in the biggest bar Squaring makes it worse: cents contributes millions per pair, frequency single digits.
🔍 Click to zoom - drawn to scale, the cents column is the only feature the ruler can see
LiveHand-compute the takeover: one pair of customers4 min▶

No hand-waving - pick two customers from the pile and build their squared distance, feature by feature:

FeatureCustomer ACustomer BDifferenceSquared contribution
Recency (days)305020400
Frequency (orders/yr)124864
Monetary (cents)6,8009,1002,3005,290,000

The monetary column supplies 5,290,000 of a 5,290,464 total - 99.99% of the distance. A and B could have identical R and F, or wildly different R and F, and their distance would barely move. On raw units, the clustering that follows is a segmentation of wallet size with two decorative columns along for the ride.

LiveThe wallet-band failure: gifters and lapsed, filed together3 min▶

Here is the business cost, in one pairing. Among our five planted archetypes: one-time gifters average about $98 per order; lapsed big spenders average about $93. Nearly identical spend - and OPPOSITE campaigns. Gifters bought once, recently-ish, for someone else: you nudge them at gift occasions. Lapsed big spenders were loyal regulars who went quiet ~225 days ago: you send a win-back before they are gone for good.

To a cents-dominated ruler, $98 vs $93 makes them next-door neighbors. Raw 5-means duly files them into the same wallet band - one segment, one campaign, both halves wrong. This is what "confident garbage" looks like: the output renders beautifully, the clusters are real geometric clusters, and the marketing built on them fails quietly.

Real worldscikit-learn ships a demo dedicated to exactly this failure: on a real wine dataset, a k-means pipeline scores 35% agreement with the true classes unscaled and 96% scaled. Same data, same algorithm, one preprocessing step - the gap between garbage and gold. When the library's own docs demonstrate the trap, treat the fix as non-negotiable.
Part 3 · covers the fix

The one-line fix: z-scaling 5 min live

The repair is one line and two operations per feature: subtract the mean, divide by the standard deviation. After z-scaling (standardization), every feature speaks the same language: "how unusual is this value, for this feature?" A customer 2 units above the mean on frequency is exactly as remarkable as one 2 units above the mean on monetary - cents, days, and counts all converted into the common currency of unusualness.

LiveWhat z-scaling does, decoded3 min▶

Subtract the mean: every feature now centers on zero - "typical" is the same place for all three. Divide by the standard deviation: every feature's spread becomes one unit - a step of 1 means "one typical customer-to-customer gap", whether the raw column ran 0.2-25 or 4,500-12,000. The cents column's million-point squared contributions collapse to the same scale as everyone else's, and recency and frequency get their vote back.

This is why z-scaling is THE default preprocessing for k-means and for PCA (session 6): both are built on squared distances or squared deviations, so both inherit the biggest-unit-wins bug in raw form.

Caution for session 6, filed now scikit-learn's PCA auto-CENTERS your features (subtracts the mean) but does NOT scale them. Feed it raw units and the "biggest direction of variance" will simply be the biggest-unit column, mathematically dressed up. Centering is free; scaling is on you. Many a published "first principal component" is secretly a units artifact.
Self-studyScaling is a decision too2 min▶

One honest paragraph before the lab: z-scaling is not "removing bias", it is CHOOSING a different weighting - namely, equal voice per feature. Sometimes that is not what the business wants. If Mango Lane's strategy says recent behavior matters twice as much as spend, you can scale and then deliberately up-weight recency - and that is a legitimate, defensible design choice, as long as it is written down and owned by a human. The crime is not weighting; the crime is letting a billing export's unit convention do the weighting silently. Scaled-equal is the honest default; deliberate re-weighting is the honest exception.

Everyone runs it

The scaling lab 🥭 ★ 20 min · everyone flips the lever

The lab below runs REAL k-means (best of five k-means++ restarts) on the actual 300-customer Mango Lane sample, in your browser, with a REAL silhouette score - nothing is canned. It opens exactly as the data arrived from billing: monetary in cents. One toggle stands between you and the five planted segments.

Look at the raw scatter first. Recency runs along one axis, cents along the other. Notice the shape of the cloud before any algorithm touches it - and notice which axis your eye is already using to group people.

Run 5-means on the raw data. Silhouette 0.53, truth match 70%. Read the segment readout: the clusters are wallet bands - slices of monetary value - and the gifters and lapsed big spenders have been merged into one segment, exactly the failure Part 2 predicted.

Inspect WHY. The readout explains it: on raw units, distance is money. The cents column supplied virtually all of every squared distance, so the algorithm - working perfectly - segmented the only feature it could see.

Flip ⚖️ Scale features ON and run again. Silhouette 0.68, truth match 100%. The five planted segments come back exactly: VIP regulars, steady mid-basket, one-time gifters, fresh arrivals, lapsed big spenders. Same customers, same algorithm, same k - different ruler.

Say the sentence out loud: one preprocessing line beat any algorithm choice. No amount of k-tuning or algorithm-swapping on raw units would have gotten you what the scaling toggle just did.

Remember the honesty rail. You watched truth match jump 70% to 100% only because our customers are simulated and the truth is known. On a real project that score does not exist - you would have shipped the wallet bands, confidently. The habits from this session are the substitute for the score you will never have.

✦ Keep the before/after Screenshot or note the two readouts: raw 0.53/70% vs scaled 0.68/100%. It is the single most persuasive exhibit in this course when a colleague asks why you are "fussing with preprocessing" - and you will want it again in the capstone, where this exact ladder is step one.
Before next session

Homework 📝 ◐ 20 minutes

Check yourself

Three questions before you go 🎯 ◐ 90 seconds

1 · Features arrive with raw ranges of 1-350, 0.2-25, and 4,500-12,000. Unscaled, Euclidean distance between customers is effectively decided by...

Differences get squared before summing: a cents gap of 2,300 contributes 5,290,000 while a frequency gap of 8 contributes 64. The biggest-unit feature does not just win, it wins squared - distance IS money on our raw data.

2 · Z-scaling (standardization) transforms each feature by...

Center on zero, spread of one. After z-scaling, a 2-unit gap means "two typical customer-to-customer spreads apart" on ANY feature - cents, days, and counts all converted into the same currency of unusualness.

3 · Raw 5-means merged one-time gifters (~$98 avg order) with lapsed big spenders (~$93) - two groups needing opposite campaigns. Why did raw units cause exactly this merge?

On raw units the ruler could only see money, and $98 vs $93 is a rounding error in cents-space. The features that distinguish them - one order long ago vs many orders then silence - were mathematically present but squared into irrelevance. Scaling restored their vote and the merge dissolved.

Coverage, honestly

What this session covered from the official universe

✓Google ML Clustering course - similarity designthe "similar = nearby is a choice" framing and why similarity gets more design time than the algorithm
✓scikit-learn "Importance of feature scaling" demothe 35% unscaled vs 96% scaled result, re-enacted on Mango Lane with the cents trap
◐scikit-learn PCA - centers but does not scalefiled as a caution today; the full PCA treatment lands in session 6

Certificates, videos, and graded assessments stay with the official providers - this course teaches the working core with a live in-browser lab.

Session 2 cheat sheet

The trench coatEvery clustering algorithm is a distance calculator underneath. Whoever controls the distance controls the segments.
Euclidean distancePer-feature differences → square each → sum → square root. The default ruler of this course and of k-means.
Similar = nearbyA design CHOICE, not a fact. Features, units, and weights decide who counts as neighbors before any algorithm runs.
The cents disasterRanges 1-350, 0.2-25, 4,500-12,000. Squared, cents contributes millions vs single digits: distance IS money.
The wallet-band failureGifters (~$98) merged with lapsed big spenders (~$93) - identical spend, OPPOSITE campaigns, one wrong segment.
Z-scalingSubtract mean, divide by std. Every feature now speaks "how unusual is this?" - THE default prep for k-means and PCA.
The lab ladderRaw: silhouette 0.53, truth 70%. Scaled: 0.68, 100%. One preprocessing line beat any algorithm choice.
Two cautionssklearn PCA centers but does NOT scale. And scaling is itself a weighting decision - make it deliberately, in writing.