Logo

How we built the “item pairing” feature on a food delivery platform

Lokesh Lohar, Senior Data Analyst

Fri Jun 06 2025

6 min

Data Science, Item Pairing, Association Rule Mining, Apriori Algorithm

Imagine you’re booking food on a food delivery platform. As you add a Pizza dish to your cart, the app suggests a perfect pairing—say, Cheesy Dip or Cold Drink. This isn’t guesswork. It’s driven by intelligent recommendations powered by association rule mining.

In this blog, we’ll walk through how we implemented a cart-level menu recommendation system using the Apriori algorithm, leveraging historical order data from the platform. This system runs per restaurant, ensuring personalized and relevant suggestions. Item Pairing Sample


Problem Statement

While users can order food during their train journeys, helping them discover relevant item combinations at the cart level can:

Technical Process

1. Data Pipeline

Data Sources:

Key Steps:

  1. Data Extraction:
    • Gather the transaction orders data for the last 3 months from all the restaurants.
    • Filters: Active restaurant/items only, orders with ≥2 items (to enable association analysis).
  2. Data Transformation:
    • Pivot Tables: Convert transactional data into a boolean matrix format (order_id × item_id) for association rule mining.
    • Item Metadata Enrichment: Merge with menu data (food type, item name, popularity) for post-processing.

2. Association Rule Mining (Apriori Algorithm)

Purpose:

Identify items frequently bought together using historical transaction patterns.

Workflow:

  1. Frequent Itemset Generation:
    • Apriori Algorithm (mlxtend library): Discovers item combinations with ≥1% support (i.e., appearing in ≥1% of orders).
    • Example: If “Samosa” and “Tea” appear together in 3% of orders, they form a frequent itemset.
  2. Rule Extraction:
    • Metrics:
      • Confidence: It assesses the likelihood that an item Y is purchased when item X is purchased. It provides insight into the strength of the association between two items. Confidence tells us how often items go together. Likelihood of consequent being bought if antecedent is in the cart (e.g., 80% confidence that Tea is bought with Samosa).
      • Lift: Lift evaluates how much more likely two items are to be purchased together compared to being purchased independently. A lift greater than 1 suggests a strong positive association. Lift shows how strong the connection is between items. (“Bread and butter are much more likely to be bought together than by chance.”)
    • Constraints:
      • Rules limited to 1 antecedent → 1 consequent (simpler recommendations).

3. Post-Processing & Filtering

Dietary Compatibility:

Food-Type Fallback:


4. Hybrid Recommendation Engine

Combines two strategies for full coverage:

  1. Apriori-Powered Rules: Direct item-to-item associations (high confidence).
  2. Food-Type/Popularity Rules: Fallback for items without associations.

Example Workflow:


Technical Tools Used

  1. Data Storage & Retrieval:
    • MySQL: Stores transactional data (orders, order_items), menu items, and restaurant metadata.
    • SQLAlchemy: Connects Python to MySQL for query execution.
  2. Data Processing:
    • Pandas: Merges datasets, filters orders, and transforms data (pivot tables).
    • NumPy: Handles matrix operations.
  3. Machine Learning:
    • MLxtend: Implements the Apriori algorithm and association rule extraction.
  4. Logging & Monitoring:
    • Python Logging: Tracks errors (e.g., failed restaurant processing) and performance metrics.
  5. Deployment:
    • Batch Processing: Restaurants are processed individually to avoid memory overload.
    • MySQL Connector/Python: Updates the items table with recommendations in JSON format.

Key Challenges & Solutions

  1. Sparse Data for Small Restaurants:
    • Solution: Fallback to food-type/popularity recommendations.
  2. Dietary Rule Enforcement:
    • Solution: Post-merge filtering using item_type(veg/non-veg) metadata.
  3. Scalability:
    • Solution: restaurant-level parallelization

Outcome


Future Enhancements

  1. Collaborative Filtering: Uses patterns from similar users or orders to recommend pairings for personalization.
  2. Real-Time Rules: Use streaming engines (Apache Kafka/Spark) for live updates.
  3. Deep Learning: Train embeddings for item co-occurrence (e.g., Word2Vec).
  4. Contextual Filtering: Add contextual filters based on time of the day, order size, etc.
  5. Real-time feedback loop: User clicks on suggested items and add-to-cart behavior helps refine what gets shown.

This system balances rule-based logic with machine learning to deliver scalable, context-aware recommendations, enhancing user engagement on the platform.

logo_image

100+ million lives touched