0:00Intro: MOFUSA Tech Academy
Welcome to MOFUSA Tech Academy. Beyond learning, towards mastery.
0:09Which products go together? The hook
Every online shop wants to know which products go together. Put them in a bundle, place them next to each other, or show "customers who bought this also bought…" at checkout. Here are twelve Brightcart orders. At first glance, there's a rule: more than a third of the customers who buy bread also buy milk. Sounds like a bundle opportunity. It's not. In fact, bread buyers are less likely to buy milk here, not more. To see why, we need three numbers that every market analyst uses: support, confidence and lift. And an algorithm with a clever shortcut, called Apriori, that finds the good rules without checking every possible combination.
Season One, Module Five, stop three: association rules.
1:02Itemsets, transactions and rules
A few words first. An item is a single product. An itemset is a group of items, like bread and butter. Each basket is called a transaction. And a rule has the form "if A, then B": if bread is in the basket, butter tends to be in it too. We can write the baskets as a table of ticks: one row per order, one column per product. There's nothing to predict and no labels. The patterns come straight from the baskets, which makes this another kind of unsupervised learning.
1:38Support, confidence and lift
Three numbers describe every rule. Support measures how common something is: the share of all baskets that contain it. Bread appears in eight of twelve baskets, a support of zero point six seven. Bread and butter together appear in six, a support of a half. Confidence measures how reliable a rule is: of the baskets that contain A, what share also contain B? It's a conditional probability, from episode four. Of the eight baskets with bread, six also have butter. Confidence: seventy-five percent. But confidence has a blind spot, so we need the third number.
Lift compares the rule's confidence with how common B is anyway. Butter is in seven of twelve baskets, about fifty-eight percent of all orders. Among bread buyers, it's seventy-five percent. Seventy-five divided by fifty-eight: a lift of one point two nine. Bread buyers are about twenty-nine percent more likely than average to buy butter. A lift above one means the items appear together more than chance would suggest. Exactly one means they're independent. Below one means they appear together less than by chance.
2:57The bread to milk trap
Now the rule from the start. Bread to milk: three of the eight bread baskets have milk, so the confidence is thirty-seven and a half percent. Sounds like a respectable pattern. But milk is popular on its own. Pause and predict: is buying bread good news or bad news for milk sales?
Milk is in half of all baskets. Among bread buyers, it's in fewer than four in ten. The lift is zero point seven five, below one. In Brightcart's data, a bread buyer is actually less likely than the average customer to buy milk. A bread-and-milk bundle would be built on an illusion. That's why confidence alone is dangerous: a popular item like milk gives high confidence to almost any rule pointing at it. Lift corrects for that. Now look at cereal to milk: confidence sixty-seven percent, lift one point three three, a genuinely positive association.
But it's based on just two baskets. With so little support, it could easily be chance. Strong rules need decent support and lift above one.
4:11Why we can't check every itemset
To find good rules, we first need the itemsets that appear often enough. That's harder than it sounds. With just six products, there are sixty-three possible non-empty itemsets: six singles, fifteen pairs, twenty triples, and so on. A real shop with ten thousand products has two to the power of ten thousand possible itemsets, a number with more than three thousand digits. We can't check them all. We need a shortcut.
4:44The Apriori principle
The shortcut rests on one simple fact. If an itemset is rare, any bigger itemset that contains it must be at least as rare. Every basket containing bread, butter and milk also contains butter and milk. So if butter-and-milk is rare, adding bread can't make it common. The Apriori algorithm uses this to search level by level. Let's say a frequent itemset must appear in at least three of the twelve baskets. Level one: count single items. Bananas appear in only two baskets, so they're out, and every combination involving bananas is skipped forever.
Level two: count pairs made from the five survivors. Four pairs make the cut: bread with butter, bread with jam, butter with jam, and bread with milk. Level three: we only build triples whose pairs all passed. Bread, butter and jam qualifies, and it appears in three baskets. But bread, butter and milk is rejected without even counting it, because butter-and-milk failed at level two. The same goes for bread, jam and milk. Level four: nothing left to build. Done. Finally, from each frequent itemset we generate rules and keep those with enough confidence and lift.
For example: customers who buy bread and butter also buy jam, half the time, with a lift of one point two.
6:14Using association rules, and their limits
Brightcart can use these rules in several ways: a breakfast bundle of bread, butter and jam; suggestions at checkout; placing related products next to each other on the site; even planning stock. But be careful how you read them. A rule is an association, not a cause. Bread and butter appear together, but that doesn't mean discounting butter will boost bread sales. When you search through thousands of possible rules, some will look impressive purely by chance, especially those based on just a few baskets. Set a minimum support, and check that the rules still hold on new orders.
And remember that shopping changes with the seasons, so rules need refreshing. For large datasets, a faster algorithm called FP-Growth finds the same frequent itemsets using a compact tree. And modern recommendation systems go further, learning from ratings and embeddings, ideas we'll meet in later seasons.
7:16Try it yourself: jam to bread
Let's practise. Compute support, confidence and lift for the rule "jam leads to bread". Pause now. Jam appears in five baskets, and four of them also contain bread. Support: four out of twelve, a third. Confidence: four out of five, eighty percent. Bread appears in eight of twelve baskets, about sixty-seven percent overall, so the lift is eighty divided by sixty-seven: one point two. Now flip the rule. Bread leads to jam has a confidence of only fifty percent: four of the eight bread baskets have jam. Confidence depends on direction, because it's a conditional probability.
Lift, though, is the same both ways, one point two, because it measures how much the two items appear together compared with chance, whichever you start from.
8:13From rules to recommendations
Association rules power simple recommendations: when a basket contains bread and butter, suggest the item with the strongest rule, jam. But modern recommendation systems go further. Collaborative filtering builds a big table of customers and products, and finds customers whose purchase histories look alike, so one can predict another's next purchase, the neighbour idea from k-N-N. A powerful version learns a short vector for every customer and every product, so that their dot product predicts how interested the customer will be.
Yes, the dot product from episode two again. Those learned vectors are a form of embedding, a central idea of Season Two.
8:58Sequential pattern mining
Shopping baskets ignore time, but customers buy in sequences. Someone who buys a kettle often buys descaling tablets a few weeks later, and a replacement filter months after that. Sequential pattern mining finds frequent ordered patterns across many customers, like "kettle, then descaler, within eight weeks". Brightcart can use them to send a helpful reminder at exactly the right moment. In Season Two, sequence models learn patterns like these automatically.
9:33Evaluating recommendations
How do you know if recommendations are any good? Offline, hide each customer's most recent purchase and check whether the recommender would have ranked it in its top five. Online, run an experiment: show the new widget to some customers and the old one to others, and compare clicks, additions to the basket, revenue, and even return rates. Watch out for popularity bias: recommending best-sellers to everyone looks safe but adds little. And beware feedback loops: recommended items get bought more, which makes them look even more recommendable, crowding out everything else.
Good recommenders balance relevance with novelty and diversity.
10:16Scaling up with FP-Growth
Brightcart's twelve baskets were tiny. With ten thousand products, there are about fifty million possible pairs alone. Apriori's pruning helps a lot, and the FP-Growth algorithm goes further, compressing all the baskets into a tree of shared prefixes, bread, then butter, then jam, and finding frequent itemsets without generating candidates at all. Practical tips for big catalogues: raise the minimum support, group products into categories first, like "breakfast spreads", and mine each season separately, because patterns change.
10:55Which rules actually matter
Mining can produce thousands of rules, and many are obvious: people who buy a printer buy ink. The valuable ones are both reliable and actionable: decent support, lift above one, enough confidence, and a business angle, like good margins or a clear moment to act. And treat every rule as a hypothesis. Before redesigning the shop, test the bundle or the placement with an experiment, and let the results decide.
11:26Pause and predict: the perfect rule from two baskets
Here's a rule that looks perfect: bananas lead to milk. Every basket with bananas also had milk, so the confidence is one hundred percent, and the lift is two. Is this the best rule in the shop? Pause. Look at the evidence: just two baskets. One more banana shopper who skips milk, and the confidence drops to sixty-seven percent. Rules built on tiny counts are mostly luck. That's exactly why rule mining sets a minimum support first, and why good teams check their rules again on fresh data before acting on them.
12:08Try it yourself: does direction matter?
Bread leads to milk with a confidence of zero point three seven five and a lift of zero point seven five. What happens if we reverse the rule, milk leads to bread? Pause and compute. The confidence changes to a half, because now we divide by how often milk appears, not bread. But the lift stays at zero point seven five. Confidence depends on direction; lift doesn't, because it compares the pair with what we'd expect if the two items were independent. Either way, the verdict is the same: these two items appear together less often than chance would predict.
12:51Myth vs Fact
Myth: a rule with decent confidence is a good rule. Fact: confidence can be high just because the consequent is popular. Always check lift. Bread to milk looked reasonable on confidence, and its lift showed the opposite of what it seemed to say.
13:08Recap
Let's recap. Support says how common an itemset is. Confidence says how often B appears when A does. And lift compares that with how common B is anyway: above one means the items go together, below one means they don't. The Apriori principle lets us skip any itemset containing a rare subset, searching level by level. And rules are associations, not causes, so watch out for rules built on tiny support. One sentence to remember: confidence can flatter, but lift tells the truth.
13:44Next: anomaly detection
Mission complete. Brightcart's breakfast bundle is bread, butter and jam, backed by lift above one, and the bread-and-milk idea is quietly dropped. Next, the last stop in unsupervised learning, and one of the most valuable. An account that usually spends about forty-seven dollars just placed a four-hundred-and-twenty-dollar order. Is that a happy customer, or a stolen card? How do machines spot the odd one out? Anomaly detection, next. See you on the map.