Lesson 3 · Real-World Data Analytics
Market Basket Analysis in Python: Support, Confidence & Lift | Data Analytics #3
Which products sell together? We build market basket analysis from scratch in pandas on real UK retail invoices: support, confidence and lift calculated by…
- CourseReal-World Data Analytics
- Lesson3 of 100
- FormatJupyter notebook · 30 code cells
- Data1 dataset
What you'll learn
Datasets used in this lesson
Save these next to the notebook. In Google Colab, upload them with the 📁 icon on the left first.
- online_retail_clean.csv preview36.2 MB
📓 Full notebook
Download .ipynbData Analytics 100, Video 3: Market Basket Analysis and Association Rules#
- Video three of the hundred-video real-world data analytics series.
- Real support, confidence, and lift, built from scratch, on real invoices from the same cleaned retail data.
- Let's get into it.
This lesson is for learners who want to find out which products customers buy together, and to understand the maths behind it rather than relying on a black-box library. The business question is: which product pairs should a retailer bundle, place side by side or recommend at checkout? You will work with the cleaned UK online gift retailer data from Video 1, online_retail_clean.csv, focusing on InvoiceNo (one invoice is one basket), Description (the product) and Country.
Market basket analysis uses three measures. Support is how often an item or pair appears across all invoices. Confidence is how often B is bought when A is bought. Lift compares that with what you would expect by chance: above 1 means the pair is bought together more often than random.
In this lesson you will:
- Build baskets of products per invoice and count every product pair.
- Calculate support, confidence and lift from scratch with Python's
Counteranditertools.combinations. - Filter out rare pairs, rank rules and turn the strongest into a bundle idea.
Prerequisites: you should be comfortable with pandas groupby, Python loops and dictionaries.
Part 1: What Market Basket Analysis Actually Measures#
Market basket analysis looks at which items appear on the same invoice, so you need the cleaned transactions plus two tools from Python's standard library. itertools.combinations generates every pair from a set of items, and collections.Counter is a dictionary that counts how often each key appears. The code loads the cleaned file with parse_dates=['InvoiceDate'] and checks its shape. The output (391150, 9) matches the cleaned dataset from Video 1, so you are starting from trusted data.
import pandas as pd
from itertools import combinations
from collections import Counter
clean = pd.read_csv('online_retail_clean.csv', parse_dates=['InvoiceDate'])
clean.shape
Part 2: Restricting to Real UK Invoices#
Buying patterns can differ by country, and mixing a small number of foreign orders with a large UK base can blur results. Restricting to one market also keeps the analysis focused. The code filters rows where Country equals 'United Kingdom'. Only the last line is displayed: uk['InvoiceNo'].nunique(), the number of distinct UK invoices, which is 16,579. Each of those invoices is one shopping basket, and this count becomes the denominator for every support figure later.
uk = clean[clean['Country'] == 'United Kingdom']
uk.shape[0]
uk['InvoiceNo'].nunique()
Part 3: Real Top Products Worth Analyzing#
Counting every possible pair across thousands of products would create a huge, noisy table. Focusing on popular items keeps the analysis fast and the results meaningful. The code groups UK rows by Description, counts the number of distinct invoices each product appears on with nunique(), sorts from highest to lowest and keeps the top 40. These names are stored in top_set, a Python set, which makes membership checks fast. The last line is an assignment, so there is no output; run top_products.head(10) on its own to see the most popular products.
top_products = uk.groupby('Description')['InvoiceNo'].nunique().sort_values(ascending=False).head(40)
top_products.head(10)
top_set = set(top_products.index)
Part 4: Building the Real Baskets#
A basket is simply the set of products on one invoice. The code keeps only rows whose product is in top_set using .isin(), then groups by InvoiceNo and applies set to the descriptions. Using a set rather than a list means a product appearing twice on the same invoice is counted once. The output shows 11,365 invoices contain at least one of the top 40 products. Each entry in baskets is now a collection of product names for one order.
in_top = uk[uk['Description'].isin(top_set)]
baskets = in_top.groupby('InvoiceNo')['Description'].apply(set)
len(baskets)
Part 5: Keeping Only Real Multi-Item Baskets#
A basket with only one item cannot tell you anything about what is bought together, so it adds nothing to pair counts. baskets.apply(len) returns the number of products in each basket, and the filter keeps those with 2 or more. The output shows 7,716 multi-item baskets remain from 11,365. Note that the count is based on top-40 products only, so a basket can have many items in total but still drop out here if only one of them is in the top 40.
baskets = baskets[baskets.apply(len) >= 2]
len(baskets)
Part 6: Real Item Support#
Support for a single item is the share of invoices that contain it, and it is the building block for lift. The code loops through every basket and adds 1 to item_counts for each item it contains. Each count is then divided by n_invoices, all 16,579 UK invoices, to give support. The output lists the top five: WHITE HANGING HEART T-LIGHT HOLDER appears in about 9.6% of invoices, followed by JUMBO BAG RED RETROSPOT at about 8.0%. Note the counts come from multi-item baskets only, so these are slightly conservative.
n_invoices = uk['InvoiceNo'].nunique()
item_counts = Counter()
for basket in baskets:
for item in basket:
item_counts[item] += 1
support = {item: cnt / n_invoices for item, cnt in item_counts.items()}
sorted(support.items(), key=lambda x: -x[1])[:5]
Part 7: Real Pair Co-Occurrence Counts#
Pair counts are the heart of the method: how many baskets contain both A and B. For each basket, combinations(sorted(basket), 2) produces every unique pair of items. Sorting first means the pair is always stored in alphabetical order, so (A, B) and (B, A) are not counted separately. The output shows 780 distinct pairs were seen. With 40 products there are exactly 780 possible pairs, so every combination occurred at least once, which you will confirm later.
pair_counts = Counter()
for basket in baskets:
for a, b in combinations(sorted(basket), 2):
pair_counts[(a, b)] += 1
len(pair_counts)
Part 8: Real Pair Support#
Pair support turns a raw count into a share of all invoices, so you can compare it with single-item support. The code divides each pair count by n_invoices with a dictionary comprehension and shows the top five. JUMBO BAG PINK POLKADOT with JUMBO BAG RED RETROSPOT leads at about 3.05% of invoices, followed by two lunch bag pairs at about 2.9% and 2.8%. Most top pairs are colour or design variants of the same product, a common pattern when shoppers buy a matching set.
pair_support = {pair: cnt / n_invoices for pair, cnt in pair_counts.items()}
sorted(pair_support.items(), key=lambda x: -x[1])[:5]
Part 9: Real Confidence, Both Directions#
Confidence answers 'of the baskets with A, what share also contain B?'. It is the pair count divided by the count for A. The function sorts the two names to look up the pair, then divides by item_counts[a]. The code picks the most frequent pair and calculates confidence both ways. The output, (0.663, 0.382), means about 66% of baskets with the pink polka dot jumbo bag also have the red retrospot one, but only about 38% of red retrospot baskets have the pink. The direction matters, as a later step explains.
def confidence(a, b):
key = tuple(sorted((a, b)))
return pair_counts[key] / item_counts[a]
example_a, example_b = sorted(pair_counts, key=lambda p: -pair_counts[p])[0]
round(confidence(example_a, example_b), 3), round(confidence(example_b, example_a), 3)
Part 10: Real Lift#
Confidence can look high just because B is popular. Lift corrects for that: it divides pair support by the product of the two single supports, which is what you would expect if the items were bought independently. A lift of 1 means no relationship; above 1 means they go together more than chance. The output for the jumbo bag pair is 8.29, so the two bags appear together about eight times more often than chance would predict. Lift is the same in both directions.
def lift(a, b):
key = tuple(sorted((a, b)))
return pair_support[key] / (support[a] * support[b])
round(lift(example_a, example_b), 2)
Part 11: Assembling One Real Rules Table#
Having every measure in one table makes sorting, filtering and saving easy. The code loops over all pairs in pair_counts and builds a tuple of the two products, the co-occurrence count, pair support, confidence in both directions and lift. pd.DataFrame(rows, columns=[...]) turns the list into a table. The output confirms 780 rows, one per pair. Storing both confidence directions saves you recalculating them when you want to read a rule the other way round.
rows = []
for (a, b), cnt in pair_counts.items():
rows.append((a, b, cnt, pair_support[(a, b)], confidence(a, b), confidence(b, a), lift(a, b)))
rules = pd.DataFrame(rows, columns=['A', 'B', 'co_count', 'support', 'conf_A_to_B', 'conf_B_to_A', 'lift'])
rules.shape[0]
Part 12: Real Top Rules by Lift#
Sorting by lift surfaces the strongest associations. sort_values('lift', ascending=False).head(10) shows the top ten. ALARM CLOCK BAKELIKE GREEN with ALARM CLOCK BAKELIKE RED has the highest lift, about 16.5, appearing together in 454 baskets. Next come the two white wooden frames (lift about 13.0) and the large and small HEART OF WICKER (about 10.8). Again these are close variants of the same product, which tells you customers often buy items as matching sets.
rules.sort_values('lift', ascending=False).head(10)
Part 13: Real Top Rules by Confidence#
Confidence ranks rules by how reliable the 'if A, then B' pattern is. The code sorts by conf_A_to_B and shows the product names, confidence and lift. The green alarm clock leads again: about 70% of baskets with it also contain the red one. The pink and red jumbo bags follow at about 66%. Notice several lunch bags pointing to LUNCH BAG RED RETROSPOT: that item is very popular, so confidence towards it is high even where lift is lower, around 7. Always check lift alongside confidence.
rules.sort_values('conf_A_to_B', ascending=False).head(10)[['A', 'B', 'conf_A_to_B', 'lift']]
Part 14: Filtering Out Real Rare Coincidences#
A pair seen only a handful of times can show a huge lift by pure coincidence, so analysts set a minimum count. The code keeps rules with co_count >= 20. The count line is not displayed, but a later step shows 779 rules pass this threshold. The displayed top ten by lift is identical to the unfiltered list, because the strongest pairs here are also common. In a larger catalogue the difference is usually dramatic. Tip: choose the threshold before looking at results, so you are not tempted to tune it to the answer you want.
meaningful = rules[rules['co_count'] >= 20]
meaningful.shape[0]
meaningful.sort_values('lift', ascending=False).head(10)
Part 15: Interpreting One Real Rule in Plain Language#
Numbers in a table need translating before a manager can act on them. The code takes the top meaningful rule with .iloc[0] and prints a sentence using an f-string, rounding lift to one decimal. The output reads: customers who buy the green bakelike alarm clock are 16.5 times more likely to also buy the red one than chance would predict. Careful: lift describes co-occurrence, not cause. It tells you the items go together, not that showing one will make people buy the other.
top_rule = meaningful.sort_values('lift', ascending=False).iloc[0]
print(f"Customers who buy '{top_rule['A']}' are {round(top_rule['lift'], 1)}x more likely to also buy '{top_rule['B']}' than random chance would predict.")
Part 16: Real Average Lift as a Baseline#
A baseline helps you judge whether a lift value is unusually high. The code calculates the mean lift across all rules (not displayed; run it alone to see it), then counts rules with lift above 2. The output is 426, so more than half of the 780 pairs are bought together at least twice as often as chance. That is high, and it partly reflects choosing the 40 most popular products, which tend to appear together in large orders. Compare top rules against this baseline rather than against 1 alone.
rules['lift'].mean().round(2)
(rules['lift'] > 2).sum()
Part 17: Visualizing the Real Strongest Associations#
A horizontal bar chart is the clearest way to present the top associations, because long product names fit on the vertical axis. The code takes the top 15 meaningful rules by lift and builds short labels from the first 15 characters of each name. plt.barh draws the bars, and the [::-1] slices reverse the order so the strongest rule appears at the top. The chart is saved to market_basket_top_lift.png. Look for the alarm clock pair leading clearly, followed by the frames and wicker hearts.
import matplotlib.pyplot as plt
top15 = meaningful.sort_values('lift', ascending=False).head(15)
labels = [f"{a[:15]}.. + {b[:15]}.." for a, b in zip(top15['A'], top15['B'])]
plt.figure(figsize=(9, 7))
plt.barh(labels[::-1], top15['lift'][::-1], color='darkorange')
plt.xlabel('Real Lift')
plt.title('Real Top 15 Real Product Associations by Lift')
plt.tight_layout()
plt.savefig('market_basket_top_lift.png', dpi=120)
plt.close()
Part 18: Real Support vs Real Lift#
Plotting support against lift for every pair shows whether strong associations are also common ones. plt.scatter draws one dot per rule, with alpha=0.5 so overlapping dots show as darker areas. The image is saved to market_basket_support_vs_lift.png. Look for the top-left area, where pairs are uncommon but have high lift, and the top-right, where pairs are both frequent and strongly linked. Those top-right pairs are the most valuable for business decisions, because they affect many baskets.
plt.figure(figsize=(8, 6))
plt.scatter(rules['support'], rules['lift'], alpha=0.5, color='teal')
plt.xlabel('Real Support')
plt.ylabel('Real Lift')
plt.title('Real Support vs Real Lift Across All Candidate Pairs')
plt.tight_layout()
plt.savefig('market_basket_support_vs_lift.png', dpi=120)
plt.close()
Part 19: Which Real Product Shows Up in the Most Strong Rules#
Some products link to many others, making them good anchors for displays or bundles. strong keeps meaningful rules with lift above 2. pd.concat stacks the A and B columns into one long list of product names, and value_counts() counts how often each appears. SET/5 RED RETROSPOT LID GLASS BOWLS and GARDENERS KNEELING PAD KEEP CALM lead with 33 strong rules each, followed by the cake cases and the pantry recipe box with 32. These are your best-connected products across the catalogue.
strong = meaningful[meaningful['lift'] > 2]
appearance_counts = pd.concat([strong['A'], strong['B']]).value_counts()
appearance_counts.head(10)
Part 20: A Real Bundle Recommendation#
Turning the analysis into a concrete recommendation is the final goal. The code takes the most connected product, the red retrospot glass bowls, and filters strong to rules where it appears as either A or B, using | (or). It then sorts by lift. The output lists partners such as PACK OF 72 RETROSPOT CAKE CASES, SET OF 4 PANTRY JELLY MOULDS and RECIPE BOX PANTRY YELLOW DESIGN; the full table is long and truncated in the stored output. Together these suggest a 'retro baking' bundle built around the bowls.
most_connected = appearance_counts.index[0]
bundle_candidates = strong[(strong['A'] == most_connected) | (strong['B'] == most_connected)]
bundle_candidates[['A', 'B', 'lift']].sort_values('lift', ascending=False)
Part 21: Saving the Real Rules Table#
Saving the rules lets a merchandising team or a later lesson use them directly. The code sorts meaningful rules by lift and writes them to market_basket_rules.csv with index=False. It reloads the file and checks the row count matches. The output True confirms nothing was lost. Saving the sorted version means anyone opening the file in a spreadsheet sees the strongest rules first.
meaningful.sort_values('lift', ascending=False).to_csv('market_basket_rules.csv', index=False)
reloaded_rules = pd.read_csv('market_basket_rules.csv')
reloaded_rules.shape[0] == meaningful.shape[0]
Part 22: Real Basket Size Distribution#
Basket size helps explain the results: bigger baskets create more pairs. baskets.apply(len) counts items per basket. The value_counts().sort_index() line gives the full distribution but is not displayed. The output is the mean: 4.15 top-40 products per multi-item basket. A basket of 4 items yields 6 pairs, so larger orders contribute a lot of pair counts. Run the distribution line on its own to see whether a few very large baskets dominate.
basket_sizes = baskets.apply(len)
basket_sizes.value_counts().sort_index()
basket_sizes.mean().round(2)
Part 23: Real Observed Pairs vs Real Possible Pairs#
Comparing observed pairs with possible pairs tells you how dense the co-occurrence data is. math.comb(40, 2) gives the number of ways to choose 2 items from 40, which is 780. The middle line is not displayed; the output is the percentage of possible pairs actually observed: 100.0. So every one of the 780 possible pairs among the top 40 products appeared together in at least one basket. That density is another reason to use a minimum count and lift, rather than 'seen together' alone.
from math import comb
possible_pairs = comb(len(top_set), 2)
len(pair_counts), possible_pairs
round(len(pair_counts) / possible_pairs * 100, 1)
Part 24: Manually Verifying One Real Support Value#
When you write calculations by hand, it is good practice to verify one value a different way. The code loops through every basket and counts those that contain both example products, using a generator inside sum(). It then compares that number with the stored pair count. The output True shows the two methods agree. A spot check like this catches subtle bugs, such as forgetting to sort pair names before lookup.
manual_count = sum(1 for basket in baskets if example_a in basket and example_b in basket)
manual_count == pair_counts[tuple(sorted((example_a, example_b)))]
Part 25: Why Confidence Is Not Symmetric#
Confidence divides by the count of the first item, so the two directions differ whenever the items have different popularity. The first line is not displayed, but the printed sentence shows the numbers: JUMBO BAG PINK POLKADOT appears in 763 baskets and JUMBO BAG RED RETROSPOT in 1,326. The pair count is the same in both directions, but dividing it by 763 gives a much higher share than dividing by 1,326. That is why confidence from pink to red is about 0.66 and from red to pink only about 0.38.
item_counts[example_a], item_counts[example_b]
print(f"'{example_a}' appears in {item_counts[example_a]} real baskets; '{example_b}' appears in {item_counts[example_b]}, which is exactly why the two real confidence values differ.")
Part 26: Real Lift Value Distribution#
A histogram of lift shows how unusual the strongest rules are compared with the rest. plt.hist uses 30 bins, and plt.axvline(1, ...) draws a dashed red line at lift 1, the point where items are bought together exactly as often as chance predicts. The chart is saved to market_basket_lift_distribution.png. Look at how much of the distribution sits to the right of that line, and how long the right-hand tail is. With 426 rules above 2, most of the bars should be well past the red line.
plt.figure(figsize=(8, 5))
plt.hist(rules['lift'], bins=30, color='slateblue', edgecolor='white')
plt.axvline(1, color='red', linestyle='--', label='Real independence (lift = 1)')
plt.xlabel('Real Lift')
plt.ylabel('Real Number of Rules')
plt.title('Real Distribution of Lift Across All Candidate Pairs')
plt.legend()
plt.tight_layout()
plt.savefig('market_basket_lift_distribution.png', dpi=120)
plt.close()
Part 27: Saving the Real Item Support Table#
Item support is useful on its own, for example to see which products appear in the most baskets. The code sorts the support dictionary by value, turns it into a two-column DataFrame, saves it to market_basket_item_support.csv and shows the first five rows. The output matches the earlier list: the white hanging heart T-light holder at about 0.096 support, followed by the red retrospot jumbo bag at about 0.080. Saving this table means you can join it with the rules file later.
support_df = pd.DataFrame(sorted(support.items(), key=lambda x: -x[1]), columns=['Product', 'Support'])
support_df.to_csv('market_basket_item_support.csv', index=False)
support_df.head(5)
Part 27b: Real Bundle Coverage Check#
A useful question is how many baskets the strong rules actually touch: are they niche or widespread? The code checks each basket against every strong rule, using any() to stop as soon as one pair is found in the basket. It then divides by the number of multi-item baskets. The output is 92.9%: almost every multi-item basket contains at least one strongly associated pair. That shows these associations are part of everyday buying, not rare curiosities.
baskets_with_strong_rule = sum(1 for basket in baskets if any(a in basket and b in basket for a, b in zip(strong['A'], strong['B'])))
round(baskets_with_strong_rule / len(baskets) * 100, 1)
Part 27c: Real Rule Count by Minimum Support Threshold#
The minimum count threshold changes how many rules you keep, so it helps to see that trade-off directly. The code loops over thresholds of 5, 10, 20, 50 and 100 and prints how many rules meet each. All 780 survive at 5 and 10, and 779 at 20. The count falls to 643 at 50 and 231 at 100. This shows the earlier threshold of 20 removed almost nothing here, because the top 40 products are all common. A stricter threshold such as 100 leaves a shorter, more robust list.
for min_count in [5, 10, 20, 50, 100]:
n_rules = (rules['co_count'] >= min_count).sum()
print(f'min_count={min_count}: {n_rules} real rules survive')
Part 22: One Last Real Sanity Check#
A pair cannot appear in more invoices than either of its items, so pair support should never exceed single-item support. The code uses join to attach the support of product A to each rule, then checks that every rule's support is less than or equal to it with .all(). The output is True. Checks based on simple logical rules like this are a quick way to catch errors in hand-built calculations.
(rules['support'] <= rules[['A']].join(pd.Series(support, name='sA'), on='A')['sA']).all()
Wrap-Up: What You Learned#
- Support, confidence, and lift are genuinely just three plain formulas over real invoice co-occurrence counts, no library required.
- Support measures how common a real product or pair is; confidence measures a real one-directional buying pattern; lift measures whether that pattern actually beats real chance.
- A real minimum support threshold is essential; rare pairs can show a huge lift purely by coincidence.
- The real most cross-sell-connected products are the strongest real candidates for bundle placement across the whole catalog.
- For production scale across a full real catalog, the mlxtend library implements this real same apriori logic far more efficiently; the math underneath is identical to what you just built.
- Next video: real cohort analysis, tracking how real customer groups actually retain over time.
Practice on your own
- Repeat the whole analysis for invoices from Germany or France instead of the UK, then compare the top five rules by lift with the UK list.
- Change the product list from the top 40 to the top 80, rebuild
baskets,pair_countsandrules, and check how many of the possible pairs are now observed. - Using the
rulestable, list the ten rules with the biggest gap betweenconf_A_to_Bandconf_B_to_A, and explain each one using the item counts.
The next video in the series covers cohort analysis, tracking how groups of customers who started in the same month actually retain over time.
🧠 Test yourself
Pick an answer to check it. Your best score is saved in this browser.
In market basket analysis, what does the support of a single item measure?
Support is item count divided by total invoices: how common the item is across baskets.
Item A appears in 50 baskets. A and B appear together in 10 baskets. What is the confidence of A -> B?
Confidence(A->B) = pair count / count of A = 10 / 50 = 0.20.
A rule has a lift of 3.2. What does that mean?
Lift compares pair support to what you'd expect if the items were independent. Above 1 means a positive association; 1 means no relationship.
Why does the lesson filter rules with
co_count >= 20before ranking by lift?A pair seen only once or twice can have a giant lift by accident. A minimum count keeps rules that are backed by enough evidence.
What does this code return?
from itertools import combinations print(list(combinations(sorted({'c', 'a', 'b'}), 2)))
Sorting first gives a, b, c.
combinations(..., 2)returns each unordered pair once, in order. Sorting also keeps the pair key consistent across baskets.
💻 Practice exercises
Try each one in the notebook or in your own editor before opening the solution.
Make a list of 10 baskets (Python sets of product names such as 'Mug', 'Candle', 'Card', 'Bag'). Count how many baskets contain each item with a Counter and compute each item's support. Print items sorted by support.
💡 Hint
Loop over baskets and over items in each basket, incrementing a Counter.
✅ Show solution
from collections import Counter baskets = [ {'Mug', 'Candle'}, {'Mug', 'Card', 'Bag'}, {'Candle', 'Card'}, {'Mug', 'Candle', 'Card'}, {'Bag', 'Card'}, {'Mug', 'Candle'}, {'Candle', 'Bag'}, {'Mug', 'Card'}, {'Mug', 'Candle', 'Bag'}, {'Card', 'Candle'}, ] n_invoices = len(baskets) item_counts = Counter() for basket in baskets: for item in basket: item_counts[item] += 1 support = {item: cnt / n_invoices for item, cnt in item_counts.items()} for item, s in sorted(support.items(), key=lambda x: -x[1]): print(item, round(s, 2))
Using the same style of baskets (12 baskets, sets of items), count pair co-occurrences with
combinations, then write functionsconfidence(a, b)for both directions and print the confidence of A->B and B->A for the most frequent pair.💡 Hint
Store pair counts under a sorted tuple key and divide by the item count of the antecedent.
✅ Show solution
from collections import Counter from itertools import combinations baskets = [ {'Mug', 'Candle'}, {'Mug', 'Card', 'Bag'}, {'Candle', 'Card'}, {'Mug', 'Candle', 'Card'}, {'Bag', 'Card'}, {'Mug', 'Candle'}, {'Candle', 'Bag'}, {'Mug', 'Card'}, {'Mug', 'Candle', 'Bag'}, {'Card', 'Candle'}, {'Mug', 'Candle'}, {'Bag', 'Mug'}, ] item_counts = Counter(item for b in baskets for item in b) pair_counts = Counter() for basket in baskets: for a, b in combinations(sorted(basket), 2): pair_counts[(a, b)] += 1 def confidence(a, b): key = tuple(sorted((a, b))) return pair_counts[key] / item_counts[a] top_pair = max(pair_counts, key=pair_counts.get) a, b = top_pair print('Top pair:', top_pair, 'seen', pair_counts[top_pair], 'times') print(f'Confidence {a}->{b}:', round(confidence(a, b), 3)) print(f'Confidence {b}->{a}:', round(confidence(b, a), 3))
Build a full rules table from 15 small baskets: for every pair compute co_count, support, confidence in both directions and lift into a DataFrame. Keep only rules with co_count >= 3 and print them sorted by lift (highest first).
💡 Hint
lift = pair_support / (support[a] * support[b]).
✅ Show solution
import pandas as pd from collections import Counter from itertools import combinations baskets = [ {'Mug','Candle'}, {'Mug','Card','Bag'}, {'Candle','Card'}, {'Mug','Candle','Card'}, {'Bag','Card'}, {'Mug','Candle'}, {'Candle','Bag'}, {'Mug','Card'}, {'Mug','Candle','Bag'}, {'Card','Candle'}, {'Mug','Candle'}, {'Bag','Mug'}, {'Mug','Candle','Card'}, {'Bag','Card'}, {'Candle','Card'}, ] n = len(baskets) item_counts = Counter(i for b in baskets for i in b) pair_counts = Counter() for b in baskets: for x, y in combinations(sorted(b), 2): pair_counts[(x, y)] += 1 support = {i: c / n for i, c in item_counts.items()} rows = [] for (a, b), cnt in pair_counts.items(): pair_support = cnt / n rows.append((a, b, cnt, pair_support, cnt / item_counts[a], cnt / item_counts[b], pair_support / (support[a] * support[b]))) rules = pd.DataFrame(rows, columns=['A', 'B', 'co_count', 'support', 'conf_A_to_B', 'conf_B_to_A', 'lift']) meaningful = rules[rules['co_count'] >= 3] print(meaningful.sort_values('lift', ascending=False).round(3))
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



