● Association rule mining · Minor project

What do shoppers
buy together?

This project mines 9,835 real grocery baskets with the Apriori algorithm to uncover product pairs that appear together far more often than chance — the same idea behind “customers who bought this also bought…”. Written in plain language, with every number reproducible.

Python · pandas · mlxtend (Apriori) · matplotlib · 62 rules discovered

0baskets analysed
0distinct products
0frequent itemsets
0strong rules (lift > 1)

Every receipt is a tiny clue

One bill tells you almost nothing. Ten thousand bills tell you which products have a habit of travelling together.

Market basket analysis treats each shopping bill as a basket of items. It then asks one question over and over: “when A is in the basket, how often is B there too?”

Patterns that survive a minimum frequency become association rules, written { pasta } → { tomato sauce }. Retailers use them for shelf placement, bundles, promotions and “recommended for you” rows — but the same maths also powers fraud detection, medical co-occurrence studies, and click-stream analysis.

This project uses the classic Groceries dataset: 9,835 anonymous transactions from a real supermarket, with 169 product categories — no prices, no customer IDs, just baskets.

The catch: popularity is not the same as association. Milk appears in a quarter of all baskets, so it “predicts” almost everything. Lift fixes that by comparing the observed pair against what independence would produce.
🧺
Baskets, not rows9,835 bills · average 4.41 items
→
🔁
Apriori pruninggrow only combinations that clear 1% support
→
🧩
333 frequent itemsetssingles, pairs and larger groups
→
📏
62 rules at 40% confidencefiltered to lift > 1
→
💡
Plain-English insightwhat a shop could actually do with it
✓

Three numbers, one rule

Support, confidence and lift are the whole language of association rules. Here they are without the jargon.

S

Support — how common?

Share of all baskets that contain the combination. Low support means the pattern is too rare to act on.

1.04% of 9,835 = 102 baskets
C

Confidence — how reliable?

Of the baskets with the “if” part, how many also had the “then” part. It is the accuracy of the prediction.

102 with citrus+root → 60 with veg = 58.62%
L

Lift — how surprising?

Confidence divided by the product’s natural popularity. Above 1 means a real association; 1 means coincidence.

58.62% ÷ 19.35% = 3.03×

Build a basket, see the rules

Pick the products you are buying. The lab shows the rules from this project that apply, using the real support, confidence and lift values.

🛍️ Your basket

Tap products to add or remove them.

0 items

🔗 Matching rules

    Only 24 of the 62 discovered rules are loaded here to keep the demo light. The full table is in results/association_rules.csv.

    The patterns, visualised

    Every chart is generated by the notebook and committed with the project, so nothing is hand-drawn.

    Dots in the upper-right with a bright colour are the commercially interesting ones: common enough to matter, reliable enough to trust, and genuinely associated.

    What fills the baskets

    Support of each product on its own — the baseline every rule is measured against.

    whole milk
    25.55%
    other vegetables
    19.35%
    rolls/buns
    18.39%
    soda
    17.44%
    yogurt
    13.95%
    bottled water
    11.05%
    root vegetables
    10.90%
    tropical fruit
    10.49%

    From raw text to a business decision

    The notebook is a linear, reproducible pipeline — run it top to bottom and every output regenerates.

    01LoadRead one basket per line from the CSV
    02CleanTrim, lowercase, drop dupes and empty baskets
    03EncodeOne-hot basket matrix with TransactionEncoder
    04MineApriori at 1% support → 333 itemsets
    05ScoreRules at 40% confidence, keep lift > 1

    Run it in three commands

    Python 3.10+, a virtual environment, and about a minute of patience.

    terminal
    # clone and enter
    git clone https://github.com/rajeshwarshingade/market-basket-analysis.git
    cd market-basket-analysis
    
    # environment (Windows PowerShell)
    python -m venv .venv
    .\.venv\Scripts\Activate.ps1
    
    # install and launch
    pip install -r requirements.txt
    jupyter notebook

    The notebook is committed already executed, so charts and tables are visible even before you run it. No dataset download needed — data/groceries.csv ships with the repo.