The problem
A grocery basket says a lot about what the shopper is cooking. Dosa batter and a coconut mean chutney, so green chillies, roasted gram and curry leaves are likely next. A good suggestion saves the shopper a second order and adds to the basket; a bad one is noise they learn to ignore.
The same mining finds bundles for promotions, products to shelve together and the "often bought together" list on any product page.
The data
65 products in 8 categories with rough Chennai prices, and 5,000 past baskets from Kadai Fresh, a made-up online grocer. Product names are generic.
I generated the baskets from 11 shopping missions a Chennai kitchen would recognise, from weekly staples and a dosa breakfast to filter-coffee mornings, tea with biscuits, baking, festival sweets and a cleaning day, plus random add-ons. Each mission adds its products with its own chances, so the patterns are real but noisy. The recommender never sees the missions, only the baskets.
The model
Association rules. Apriori finds every set of up to three products in at least 0.5% of baskets, and each set gives rules such as "dosa batter → coconut" with three numbers: support (the share of all baskets holding the set), confidence (the share of baskets with the left side that also hold the right) and lift (confidence against how often the right side sells anyway). I keep rules with a lift above 1, bought together more often than chance. The most confident rule for dosa batter is coconut: in 63% of baskets with dosa batter, 3.9× more often than chance.
Item similarity. Each product is a column of the basket matrix (1 if the basket holds it), and two products' similarity is the cosine between their columns: high when they turn up together a lot, whatever their popularity. A candidate scores its summed similarity to everything in the basket.
Blend. Each score is scaled by the best candidate's and the two are averaged; the reason shown is the rule when one applies. Every method is topped up with best sellers when it has fewer than five ideas, and none ever suggests what is already in the basket.
The pipeline
- 01Generate
5,000 baskets from a fixed seed, the same on every visit.
- 02Mine
Frequent sets of up to three products and the similarity of every pair, in a Web Worker so the shop stays responsive.
- 03Filter
Rules pass the support and confidence you choose in the explorer, and a lift above 1.
- 04Score
Rules, similarity or the blend score every product not in the basket; the top five are shown.
- 05Explain
Each suggestion carries its rule or the basket product it is most like.
- 06Evaluate
A fifth of the baskets are held back; one product is hidden from each, and every method has five tries to find it.
Results
Measured when this page was built, with the same code the demo runs: trained on 4,000 baskets, tested on 986 it never saw, rules at 1.0% support and 20% confidence (4,742 rules). Every method finds the hidden product at least 1.8 times as often as the best sellers do. Similarity edges ahead because it adds up evidence from the whole basket, while a rule speaks for at most two products; the blend gives up a little of that for clearer reasons.
For a client
With a real shop's orders I would mine the last few months, refresh every night, and add what baskets cannot say: stock, margins and the season.
I would judge suggestions by a live test against the current page, measuring basket size and add-to-basket clicks, not by hit rate alone.
The rules are merchandising tools too: the strongest pairs make good bundles, and pairs with a lift well below 1 show what shoppers buy instead of each other.
Stack
Privacy
Your basket stays in this browser tab. Nothing is sent anywhere and nothing is kept after you leave.
Limits
- The baskets come from missions I wrote, so the scores show how well the methods recover those patterns, not how they would do in a real shop.
- Hit rate rewards guessing what the shopper would have bought anyway. A shop also wants suggestions the shopper had not thought of, and only a live test measures those.
- A new product has no history, so neither method can suggest it until it has sold a few times.