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

What sells together

Seven sessions asked "which customers are similar?" Today the same unlabeled pile answers a different question: "what goes together in a basket?" Association rules are the third unsupervised job from session 1 - co-occurrence - and they run on three numbers: support, confidence, and lift. Two of the three can flatter a worthless rule; one of them is honest. The lab mines 500 real Mango Lane baskets live in your browser, and by the end you will read a rule table the way a merchandiser should: lift first, support as the sanity check, confidence with suspicion.

🟡 Building Builder track Live basket mining 45 min + self-study depth
0-3 · Welcome 3-20 · Concepts 20-40 · Mine the baskets 40-45 · Q&A
Part 0

Why this session exists

Session 1 said unsupervised learning does three things to an unlabeled pile: group similar things, compress features, and find what goes together. The first two are done. The third works on a different unit - not the customer but the basket - and it powers the most visible retail analytics there is: "customers who bought this also bought...". The field's founding legend is the beer-and-diapers story: a supermarket supposedly discovered that young fathers buying diapers also grabbed beer, and moved the shelves accordingly. The story is very possibly apocryphal - nobody has ever produced the original analysis - but it survives because it captures the promise perfectly: co-occurrence patterns, mined from receipts nobody labeled, that a human merchandiser would never have guessed. Today you learn the three numbers that separate the real patterns from the folklore.

Live - covered in session Self-study - read after ✦ Lab moment 🥭 The Mango Lane case
★ What you walk out with today The anatomy of a rule (if A, then also C), fluency in its three numbers - support, confidence, lift - including the hand arithmetic, the popular-item trap that makes confidence lie, apriori decoded in one paragraph, and a live-mined rule table from 500 Mango Lane baskets that you can read like a practitioner.
The honesty rail (used all course) The lab's baskets are simulated with planted style pairs, which is why we can tell you which rules SHOULD surface. The mining itself is real - real counting over real baskets in your browser, no script. On real receipts you get the same table with no answer key; the three numbers and your skepticism are the whole toolkit.
Part 1 · covers support, confidence, lift

The three numbers of a rule 8 min live

A rule has the shape "if the basket has A, then it also has C" - antecedent, consequent. Every mined rule carries three numbers, and each answers a different question: support asks how common the pair is, confidence asks how reliable the if-then is, and lift asks whether the pairing beats plain chance. You need all three because each one alone can be gamed.

🧺 IF basket has A the antecedent · white sneakers then also... 🧺 ...C as well the consequent · crew socks SUPPORT - how common baskets with A and C divided by ALL baskets rare rule = thin evidence CONFIDENCE - how reliable baskets with A and C divided by baskets with A directional: A→C is not C→A LIFT - versus chance confidence divided by C's base rate in all baskets 1 = independent · above 1 = pull Support says how often, confidence says how reliably, lift says how surprising. Read all three.
🔍 Click to zoom - rule anatomy: antecedent, consequent, and what each number divides by
LiveThe arithmetic, worked by hand4 min▶

Toy shop, 100 baskets. Item A appears in 20 baskets, item C appears in 25, and 10 baskets contain both. The rule A → C:

NumberComputationValueWhat it just told you
Support10 both ÷ 100 baskets10%one basket in ten carries the pair - common enough to matter
Confidence10 both ÷ 20 with A50%half of A's buyers also take C - the if-then is fairly reliable
C's base rate25 with C ÷ 10025%the chance ANY basket has C, ignoring A entirely
Lift50% ÷ 25%2.0A's buyers take C at twice the base rate - a real pull, not chance

Two things to lock in. Confidence is directional: conf(A→C) = sup(A∪C) / sup(A), so flipping the rule changes the denominator - C→A here scores 10 ÷ 25 = 40%, a different number for a different claim. Lift is symmetric and centered on 1: lift 1 means A and C are independent, above 1 they attract, below 1 they actively avoid each other - a rule with lift 0.4 is telling you buyers of A do NOT buy C, which is merchandising information too.

Real worldThe mlxtend library - the standard Python toolkit for this - computes exactly these columns from exactly these ratios, plus a few research extras (leverage, conviction). When practitioners say "we mined the baskets", they mean this table. The math you just did by hand on 100 baskets is the entire method; the library's contribution is doing it fast over millions.
Self-studyDirectionality in practice: the arrow picks the inbox2 min▶

Because confidence divides by the antecedent, the two directions of one pairing are two different campaigns. In the toy above, A→C scores 50% confidence while C→A scores 40% - same pair, same lift, different reach. The direction you ship is the direction whose antecedent you can act on: a cross-sell email goes to the people who already bought the IF side, so A→C at 50% means half the A-buyers you email are already inclined toward C. Flip the arrow and you are emailing C-buyers with a weaker claim. Lift tells you the pair is real; the confidence pair tells you which side of it to stand on. Check both directions before drafting anything - it is one extra division.

Self-studyWhy the basket, not the customer2 min▶

Notice the unit change: sessions 1-7 described customers (one row each, RFM); today's rows are baskets - one receipt, a set of items, no identity required. That is a feature, not a limitation: basket mining works on anonymous transactions, needs no customer history, and answers merchandising questions (placement, bundles) that segmentation cannot. The two views compose: a win-back email to lapsed big spenders (session 7) can carry the cross-sell the basket rules suggest (today). Same pile of unlabeled data, two lenses, one campaign.

Part 2 · covers the popular-item trap

Why lift is the honest column 6 min live

Here is the trap that catches every first-time basket miner: a rule can have high confidence purely because the consequent is popular with everyone. If 70% of all baskets contain gift wrap, then "anything → gift wrap" scores 70% confidence - beanies, belts, returns of socks, all of them "predict" gift wrap at 70%. The if-part contributed nothing. Confidence flatters popular items; lift divides that popularity back out. That is the whole reason lift exists.

The popular-item trap any item → gift wrap confidence 70% - looks impressive but 70% of ALL baskets have gift wrap lift = 70% ÷ 70% = 1.0 A told you nothing about C - put gift wrap next to anything, same result A real pairing white sneakers → crew socks confidence 72% - almost the same but socks sit in only ~24% of baskets lift = 72% ÷ 24% = 3.0x sneaker buyers take socks at three times chance - a pull worth money lift is the honest column Nearly identical confidence, opposite meaning. Only the lift line can tell these two rules apart.
🔍 Click to zoom - two rules with almost the same confidence; lift separates the trap from the treasure
LiveThe reading order: lift first, support as sanity check3 min▶

The practitioner's reading order for a rule table:

Sort by lift - it is the column that measures surprise, and surprise is where the unmined money is. A merchandiser already knows the popular items.

Sanity-check support before believing anything. A lift of 20 computed on 3 baskets is noise wearing a crown - tiny denominators produce spectacular ratios. If the pair barely occurs, the rule is an anecdote, not a pattern. Chase lift, but only where support says the evidence is thick enough to stand on.

Then read confidence for operations: once lift says the pull is real and support says it is common, confidence tells you what fraction of A-buyers your cross-sell will actually reach.

LiveApriori, decoded in one paragraph2 min▶

How does a machine find these rules without checking every possible item combination (which explodes combinatorially)? Apriori's one clever idea: count frequent itemsets first, and prune the rare ones early - because if {sneakers} alone is rare, then {sneakers, socks} cannot possibly be frequent, so entire branches of the search die without being counted. Count singles, keep the frequent; count pairs built only from frequent singles; keep going until nothing survives; then derive the if-then rules from the surviving sets. Clever counting, not magic. Its faster sibling fpgrowth reaches the same rules through a compressed tree structure - same output, better engineering - and both ship in mlxtend.

Real world"Frequently bought together" panels and supermarket endcap pairings are the public face of this method - basket mining is one of the oldest deployed forms of unsupervised learning in retail, decades older than the deep learning wave. The reason it survives is the reason this course teaches it: three interpretable numbers a category manager can interrogate beat a black box they cannot.
Self-studyThe knobs: minimum support and minimum confidence2 min▶

Real mining runs need thresholds, or the rule table drowns you. The standard workflow sets them in this order:

KnobWhat it doesHow to think about it
min supportdrops itemsets that occur too rarely to trustthe evidence floor - set it first; it is also what makes apriori's pruning bite
min confidencedrops if-thens that are too unreliable to act onthe operations floor - below it, the cross-sell reaches too few A-buyers to bother
lift filterdrops rules that merely restate popularityapplied last, on the surviving table - the honesty pass from part 2

Set the floors too high and real niche pairings vanish; too low and the table fills with three-basket anecdotes. Practitioners iterate: mine, eyeball, adjust, re-mine. The knobs are judgment calls, which is exactly why the three-number fluency from part 1 matters more than the library call.

Part 3 · covers actioning rules

From rules to money 🥭 4 min live

A validated rule feeds three classic actions. Cross-sell messaging: "bought sneakers? here are socks" - the email writes itself from the rule's two sides. Placement: put the pair together, on the shelf or on the product page. Bundles: price the pair as a set and let the lift do the persuading. For Mango Lane, the win-back email from session 7's segment sheet just got smarter: it can now feature what lapsed spenders' baskets historically paired.

LiveThe honest boundary: co-occurrence is not causation2 min▶

A rule describes what already happens in baskets; it does not explain why, and it does not promise that pushing A will drag C along. Sneakers and socks may pair because one causes the other, because both serve one outfit, or because both sit in a summer sale. The rule cannot tell you which - and does not need to, IF you treat it correctly: putting socks next to sneakers is a test of the rule, and the rule alone proves nothing. Ship the placement, measure attach rate against a control, and let the experiment - not the lift column - make the causal claim. A rule licenses a cheap test; it never licenses a causal sentence in a slide deck.

And the pointer this course owes you: rules are the shallow end of "people who bought X also...". Full recommender systems - collaborative filtering, matrix factorization, the personalized feed - live in learn-recommendation-with-phoebe on the Kirana case. This session stops at rules, on purpose: they are the interpretable, arguable, merchandiser-friendly member of the family.

Self-studyWhich action for which rule2 min▶

A rough matching, worth arguing with: cross-sell messaging wants high confidence on top of real lift, because every email costs attention and you want most recipients already inclined. Placement tolerates lower confidence - putting things near each other is nearly free, so any healthy-support rule with lift comfortably above 1 earns a shelf test. Bundles demand the most: real lift, thick support, AND a margin check the rule table knows nothing about, because a discount on a pair people already buy together is money handed back. In all three cases the rule nominates the candidate and the experiment confirms it - the table is a shortlist generator, never a decision.

Self-studyRules have a shelf life2 min▶

Session 7 said segments drift; rules drift harder, because baskets follow seasons, promotions, and stock. A pairing mined during a gifting season can evaporate by spring; a rule created by last month's bundle promotion is your own marketing echoing back at you, not customer preference. Two working habits follow. First, date every mined table and re-mine on a schedule, exactly like the segment sheet's review date. Second, before shipping any rule, ask what was happening in the shop during the mining window - a sale, a launch, a holiday - and whether the rule is the customers talking or the calendar. The arithmetic is timeless; the baskets are not.

Everyone mines

Mine the baskets 🧺 ★ 20 min · live rule mining in your browser

The lab below mines association rules from 500 Mango Lane baskets the moment the page loads - real counting over real simulated receipts, computed live, no pre-baked table. The planted style pairs are in there; so are the popularity traps. Read the table the way part 2 taught you.

Know what you are looking at. The table was mined live from 500 baskets on page load - support, confidence, and lift counted for real in your browser, no script supplying answers. Reload the page and the counting runs again.

Read the top rule: white sneakers → crew socks. Support 14%, confidence 72%, lift 3.0x. Say the full sentence: the pair sits in 14% of all baskets, 72% of sneaker baskets also carry socks, and that is 3x more than sock-buying chance alone would predict. Buyers pair them - three times more than chance.

Find midi dress → leather belt (lift 2.9x). This is the second planted style pair, and it surfaced because the mining is real - nobody typed it into the page. The lab plants the pattern; the arithmetic finds it. That is the entire promise of the method, demonstrated.

Read a lower-lift row and say WHY it ranks lower. Pick any rule with decent confidence but modest lift and explain it out loud using part 2's trap: the consequent's base rate is doing the work, so the antecedent adds little pull. If you can articulate that, you can read any rule table in the industry.

Draft the cross-sell email the top rule writes for you. Subject line, one sentence, using both sides of the rule - something like "Your sneakers asked us for socks". Note how the rule hands you the WHAT (the pairing) while the segment sheet from session 7 hands you the WHO.

Run the lift-versus-confidence argument. Find any rule where confidence looks strong but lift sits near 1, and explain it via the popular-consequent trap: everyone buys that item anyway, so the rule earns no shelf move and no email. One strong-confidence row rejected out loud is the session's exit exam.

✦ The reading order, one more time Lift finds the surprise, support certifies the evidence, confidence sizes the operation. Any rule you ship should pass all three reads - and even then, it ships as a test with a control group, never as a causal claim.
Before next session

Homework 📝 ◐ 20 minutes

Check yourself

Three questions before you go 🎯 ◐ 90 seconds

1 · Support, confidence, and lift measure, in order...

Support = both ÷ all baskets. Confidence = both ÷ baskets with A (directional - flipping the rule changes the denominator). Lift = confidence ÷ C's base rate, centered on 1. Three questions, three denominators.

2 · A rule shows 70% confidence but lift almost exactly 1. What did you just learn?

Lift 1 means confidence merely equals the consequent's base rate - independence. The 70% was the item's own popularity reflected back at you. This is the popular-item trap, and lift is the column built to catch it.

3 · A high-lift, healthy-support rule licenses you to...

Rules describe co-occurrence, not causation - the pair may share a season, an outfit, or a sale. Putting socks next to sneakers TESTS the rule; only the experiment's result can support a causal sentence.

Coverage, honestly

What this session covered from the official universe

✓mlxtend association-rules documentationsupport, confidence (sup(A∪C)/sup(A), directional), and lift (1 = independent) taught to the working bar with hand arithmetic
✓apriori / fpgrowth (mlxtend frequent-patterns)the prune-the-rare counting idea decoded; fpgrowth named as the faster sibling with identical output
◐DataCamp "Unsupervised Learning in Python" ch4the NMF / recommender flavor of "what goes together" - pointed to learn-recommendation-with-phoebe rather than re-taught

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

Session 8 cheat sheet

The rule shape"If basket has A, then also C." Antecedent → consequent, mined per basket - no customer identity needed.
SupportBaskets with A and C ÷ ALL baskets. How common the pair is - and the sanity check on every spectacular ratio.
ConfidenceBaskets with A and C ÷ baskets with A. Directional: A→C and C→A divide by different things and mean different claims.
LiftConfidence ÷ C's base rate. 1 = independent, above 1 = attraction, below 1 = avoidance. The honest column.
The popular-item trapHigh confidence + lift ~1 = the consequent is just popular with everyone. Put it next to anything, same "result".
Reading orderSort by lift, sanity-check support (lift 20 on 3 baskets is noise), read confidence last for reach.
Apriori / fpgrowthCount frequent itemsets, prune the rare early, derive rules from survivors. Clever counting, not magic; fpgrowth = faster sibling.
Rules vs causationLab canon: sneakers→socks sup 14%, conf 72%, lift 3.0x. It licenses a placement TEST - never a causal claim.