Why Customers Buy Products Together
A basket of groceries contains more information than a shopping list. The combinations customers choose reveal purchasing patterns that can drive smarter recommendations, better product placement, and higher average order value — if you know how to read them.
Section 01 — The Business Problem
One purchase often predicts another
A customer buys a laptop. Within the same week, they also purchase a mouse, a keyboard, and a laptop bag. This is not a coincidence — it is a pattern. The question is whether your business can discover and act on patterns like this automatically.
A typical customer journey
If you sell 10,000 laptops and 70% of buyers purchase accessories within a week, that is a pattern worth acting on. The question is whether you know it exists.
Cross-selling
Show "frequently bought together" suggestions on the product page.
Smarter email
After a laptop purchase, send a timely accessory recommendation — not random promotions.
Inventory planning
Stock laptop bags and keyboards when laptops are promoted, because purchases correlate.
Product placement
Position accessories near laptops in-store and in category pages online.
Section 02 — The Dataset
What the data looks like
Before any analysis, it helps to understand what the raw data looks like. Purchase data is stored as transactions — each transaction is one basket, containing the items a customer bought together.
Transactions
267
Unique items
18
Avg basket size
3.0 items
Data source
Curated
Key concepts
Transaction
One purchase event. Everything a customer bought in a single visit or order. Also called a "basket."
T001: {milk, bread, eggs}Item
A single product within a transaction. Items don't have quantities here — we only record whether something was bought.
"milk", "bread", "pasta"Itemset
A combination of two or more items. We want to find itemsets that appear together more often than chance would predict.
{pasta, vegetables, red-meat}Sample transactions (7 of 267)
| Transaction | Items in basket |
|---|---|
| T001 | whole-milkbutteryogurt |
| T002 | breadbuttercoffee |
| T003 | pastavegetablesred-meat |
| T004 | cheesewineherbs-spices |
| T005 | ricechickenvegetables |
| T006 | beerred-meatsoda |
| T007 | chocolatecoffee |
Item frequency — how often each product appears
Dataset note: This is a curated 267-transaction dataset across 18 grocery items, designed to illustrate how association rule mining discovers distinct shopper profiles. For analysis on a real retail dataset, see the Python notebook which uses the UCI Online Retail Dataset .
Section 03 — Explore the Data
Loading explorer…
Section 04 — What Surprised Us
Patterns the data revealed
These findings come from running the actual analysis on our curated dataset. Every claim here is derived from the computed association rules — nothing invented.
Beer and soda: 100% confidence
In our expanded dataset, every basket containing beer also contained soda — 100% confidence, lift 8.3×. The strongest 2-item rule we found. This does not mean beer causes soda purchases. It means these two items co-occur perfectly consistently in our data.
Rice and chicken pull bidirectionally
78.9% of rice baskets include chicken. 62.5% of chicken baskets include rice. Lift 4.4× in both directions — a coherent meal-prep pattern, not a one-way recommendation. Most association rules are asymmetric; this one is not.
Popular items have weaker associations
Whole milk appears in 25% of all baskets — the most purchased item by far. But most milk-related rules have lift below 2.5. High frequency means it co-occurs with everything, so it has weak predictive value. Popularity is not the same as association strength.
Section 05 — How the Algorithm Works
Three numbers that explain everything
The Apriori algorithm discovers association rules in transaction data. You do not need to understand the algorithm to use its output — but understanding these three measurements makes every rule interpretable.
Formula
transactions containing {A and B} ÷ total transactionsExample from our data
Pasta AND vegetables appear together in 25 of 120 baskets.
Support = 25/120 = 20.8%When to use it
Use support to filter out rare combinations that might just be noise. A high-support itemset appears frequently enough to be worth acting on.
Formula
transactions containing {A and B} ÷ transactions containing {A}Example from our data
Of 20 wine transactions, all 20 also contain cheese.
Confidence = 20/20 = 100%When to use it
Confidence measures the reliability of the rule as a prediction. But a 100% confidence rule on a product that everyone buys (like whole milk) is less meaningful.
Formula
confidence ÷ (support of B alone)Example from our data
Cheese support alone = 25%. Wine → Cheese confidence = 100%.
Lift = 100% ÷ 25% = 4.0×When to use it
Lift > 1.0 means the association is stronger than chance. Lift = 1.0 means independence. Lift < 1.0 means the products are less likely to appear together than expected.
Why you always need lift
In our dataset, the rule "bread → whole-milk" has high confidence (~50%) — but lift is only 1.36. Whole milk appears in 37% of all baskets regardless. Bread barely changes the odds. Compare to "wine → cheese": confidence 100%, lift 4.0. That is a real association. Without lift, you would act on both equally. With lift, you know which one matters.
Section 06 — The Analysis Results
The buying patterns discovered
These are the actual association rules computed from our curated dataset, sorted by lift. Every number here is reproducible — run the Python notebook to verify.
Reading this table
Each row is one rule: IF a customer buys the items on the left, THEN they frequently also buy the items on the right. Rules are sorted by lift — the higher the lift, the more the association exceeds random chance.
| If customer buys | They also buy | Support | Confidence | Lift |
|---|---|---|---|---|
| Beer | Soda | 8.2% | 100% | 8.34× |
| Soda | Beer | 8.2% | 69% | 8.34× |
| Cheese | Wine | 15.0% | 80% | 5.34× |
| Wine | Cheese | 15.0% | 100% | 5.34× |
| Yogurt | Tropical Fruit | 9.4% | 68% | 4.75× |
| Tropical Fruit | Yogurt | 9.4% | 66% | 4.75× |
| Chicken | Rice | 11.2% | 63% | 4.39× |
| Rice | Chicken | 11.2% | 79% | 4.39× |
| Chicken | Herbs & Spices | 9.4% | 52% | 3.76× |
| Herbs & Spices | Chicken | 9.4% | 68% | 3.76× |
| Pasta | Vegetables | 9.4% | 76% | 3.21× |
| Vegetables | Pasta | 9.4% | 40% | 3.21× |
| Chocolate | Coffee | 7.5% | 48% | 3.03× |
| Coffee | Chocolate | 7.5% | 48% | 3.03× |
| Whole Milk | Yogurt | 10.9% | 41% | 2.95× |
Section 07 — Production Architecture
How a company would build this
Running Apriori on a CSV in a notebook is one thing. Serving live recommendations to thousands of users is another. Here is one practical architecture for taking association rules into production.
Order Data
Database / Event streamEach completed order is an event: order_id, customer_id, list of product_ids, timestamp.
Data Pipeline
ETL job · Airflow / dbtExtract orders, group by transaction, filter to complete orders, deduplicate, format as item lists.
Association Analysis
Python · mlxtend / pyfpgrowthRun Apriori or FP-Growth on the transaction list with chosen support/confidence/lift thresholds. Rerun on a schedule as new orders arrive.
Rules Store
Redis / PostgresStore the discovered rules in a fast lookup structure. Index by antecedent product ID for O(1) lookup at request time.
Recommendation API
REST / GraphQLGiven the current cart or viewed product, query the rules store and return ranked recommendations filtered by lift threshold.
Product Surface
Frontend / EmailDisplay recommendations on product pages, cart sidebars, checkout pages, or post-purchase emails.
Practical considerations
Recompute frequency
Daily batch jobs work for most businesses. Hourly for high-volume retail.
Minimum thresholds
Start with lift > 1.5. Too low = noise. Too high = too few rules.
Cold start
New products have no history. Use content-based fallbacks (category, brand) until data accumulates.
Scalability
FP-Growth is faster than Apriori at scale. For millions of transactions, consider distributed variants on Spark.
Section 08 — AI Opportunities
How AI enhances recommendations
Association rules are a foundation. AI adds context, conversation, and personalisation on top of that foundation. Neither replaces the other — they work in layers.
Example: AI-powered shopping assistant
I'm buying a gaming laptop. What else should I get?
Based on what other customers usually buy with gaming laptops, you might want a gaming mouse and keyboard — those appear together in most gaming setups. A cooling pad is also worth considering if you plan to use it for long sessions. Do you already have a monitor?
Sources used:
What the AI combines
Association rules
Historically co-purchased products, ranked by lift
Product metadata
Category, compatibility, brand relationships
Customer history
What this specific customer has already bought
Catalog context
Current inventory, promotions, pricing
Traditional rule-based
"You may also like" — shows top 4 associated products regardless of context.
Same recommendations for all customers. No conversational context.
AI-enhanced
Natural language query → combines rules + metadata + history → personalized, contextual response.
Requires more infrastructure. Rules still needed as the foundation.
Section 10 — Apply to Your Business
What patterns exist in your data?
Market basket analysis is not limited to grocery stores. Anywhere you have transaction records — features used, activities completed, courses taken, products purchased — the same technique applies.
Ecommerce
Dataset: Order history
“Which products should be recommended together?”
Order items grouped by order_id{product A} → {product B, C}Show "Frequently bought together" on product pagesSaaS Product
Dataset: Feature usage logs
“Which features are adopted together? What should be recommended next?”
Customer → features used (this month){API, SSO} → {Webhooks}Surface Webhooks in onboarding for API + SSO usersCRM / Sales
Dataset: Lead activity history
“What activities commonly occur before conversion?”
Lead → activities completed (demo, proposal, pricing...){demo, pricing page} → {closed won}When a lead sees pricing after a demo, trigger a follow-upHR / Learning
Dataset: Training completion records
“Which courses are commonly completed together?”
Employee → courses completed{Python basics, data analysis} → {SQL fundamentals}Recommend SQL when Python + data analysis are completeThe question to ask yourself
What are the “transactions” in your business? What are the “items”? If you can answer those two questions, you can run this analysis. The patterns are already in your data. You just have not looked for them yet.
References & Further Reading
Go deeper
Where this playbook ends, these resources begin.
Run the full analysis yourself
The curated dataset on this page demonstrates the concepts. The companion notebook runs the same Apriori algorithm on the UCI Online Retail Dataset — 541,909 rows, ~25,900 real transactions. Every finding can be reproduced and challenged.
Deep Reading
Mining of Massive Datasets — Chapter 6: Frequent Itemsets
Leskovec, Rajaraman, Ullman (Stanford) · 2020
Free textbook chapter covering Apriori, PCY algorithm, and efficient itemset mining at scale. The definitive reference for taking this beyond a notebook.
Find on Google Scholarmlxtend — Machine Learning Extensions
Sebastian Raschka · 2024
The Python library used in the notebook. Provides clean implementations of Apriori and FP-Growth with pandas DataFrames as input/output.
rasbt.github.io/mlxtend