Skip to content

Repository files navigation

🛒 Recommendation Systems — Apriori, FP-Growth & Naive Bayes

A full end-to-end recommendation engine built on the Global Superstore dataset, implementing and comparing three algorithms — Apriori, FP-Growth, and Naive Bayes — to generate personalised product recommendations based on customer purchase history. Accompanied by two tutorial videos published on YouTube.


📌 Project Overview

This project answers the core question of recommendation systems:

"If a user bought item X, what should we recommend next — item Y or something else?"

Three approaches are implemented and benchmarked:

  • Association Rules Mining (Apriori & FP-Growth) — Market Basket Analysis style, measuring support, confidence, and lift
  • Naive Bayes — probabilistic model answering "What is the probability a user buys item X and item Y?"
  • KNN — used as a complementary model to fill recommendation gaps left by Apriori/FP-Growth

This type of recommendation system is used in production by Amazon, Booking.com, and major airline platforms.


📁 Project Structure

├── The Final Project with Apriori & Fp-Growth Models.ipynb     # Main notebook (full pipeline)
├── The Final Project with Apriori & Fp-Growth Models (2).ipynb # Refined version
├── The Final Project with Apriori & Fp-Growth Models (3).ipynb # Extended version
├── Naive_Bayes_Model_Recommender_System.ipynb                  # Standalone Naive Bayes notebook
├── NB Model.ipynb                                              # NB evaluation & scoring
├── README.md
└── Data/
    ├── superstore.csv           # Source: Global Superstore dataset
    └── recommendations_df.csv  # Generated output: per-user recommendations

🗂️ Dataset

Global Superstore Dataset
Available on Kaggle: ronysoliman/global-superstore-dataset

Feature Description
CustomerID Unique customer identifier
ProductName Name of the ordered product
Quantity Units ordered (used to derive Rating)
Sales Revenue from the order
Profit Profit from the order
Discount Discount applied
ShipMode Shipping method
Segment Customer segment (Consumer / Corporate / Home Office)
Market Geographic market (US, EU, APAC, etc.)
OrderPriority Order urgency level
OrderDate / ShipDate Used to derive DaysofOrderPreparation

Engineered features:

  • Rating — min-max scaled from Quantity to a 1–10 rating scale
  • DaysofOrderPreparation — difference in days between order and ship dates
  • One-hot encoded Market columns
  • Label encoded ShipMode, Segment, OrderPriority

🔬 Pipeline

1. Data Wrangling & Feature Engineering

  • Renamed columns, parsed dates, dropped PII fields (CustomerName) and redundant columns
  • Cleaned CustomerID by stripping hyphens
  • Built Rating column from Quantity using min-max normalisation to a 1–10 scale
  • Applied one-hot encoding for Market and label encoding for ordinal fields
  • Produced masked correlation heatmap — Sales and ShippingCost show strongest correlation with Rating

2. Apriori Algorithm

Approach:

  • Built a customer × product pivot table (Rating values)
  • Converted ratings to binary (1 = purchased, 0 = not purchased)
  • Applied mlxtend.frequent_patterns.apriori with min_support=0.001
  • Generated association rules using lift as the metric

"Staples" handling:

  • "Staples" dominated all itemsets due to high purchase frequency
  • Re-ran Apriori excluding "Staples" to surface unique product bonds

Performance metrics visualised:

  • Support distribution (binned: Average / Above Average / Strong)
  • Confidence distribution (up to 28%)
  • Lift ratio (up to 34x)
  • Antecedent & consequent support distributions

Network diagram: built with networkx connecting antecedents → consequents for the top 15 rules

KNN gap-filling:
Users without Apriori-generated recommendations were filled using KNeighborsClassifier (k=23) trained on encoded product pairs


3. FP-Growth Algorithm

  • Same pipeline as Apriori but using mlxtend.frequent_patterns.fpgrowth
  • Faster than Apriori on large datasets (tree-based frequent pattern mining)
  • "Staples" excluded and unique itemsets extracted
  • KNN (k=23) used identically for recommendation gap-filling
  • Final output: per-customer main_recommendation + recommendation columns

4. Naive Bayes — Cluster-Based Recommender

Pipeline:

  1. Built user × product interaction matrix
  2. Applied TruncatedSVD (17 components) to reduce dimensionality
  3. Clustered users into 17 groups using K-Means
  4. Trained a separate MultinomialNB model per cluster
  5. Predicted interaction probability for every user × item pair
  6. Applied a 0.50 probability threshold to filter recommendations
  7. Exported recommendations to recommendations_df.csv

Evaluation:

  • Extracted top-3 recommended items per user for accuracy scoring
  • Scored using accuracy_score, precision_score, recall_score from scikit-learn

5. Model Comparison

Metric Apriori FP-Growth Naive Bayes
Algorithm type Association Rules Association Rules Probabilistic ML
Min support threshold 0.1% 0.1%
Gap-filling method KNN (k=23) KNN (k=23) Cluster-based NB
Accuracy measured measured measured
Speed slower faster depends on cluster size

FP-Growth is generally faster than Apriori on large datasets due to its tree-based structure avoiding repeated dataset scans.


📊 Visualisations

Visual Purpose
Treemap (top 50 countries by rating) Geographic demand overview
Masked correlation heatmap Feature relationship analysis
Bar charts (support / confidence / lift bins) Apriori performance metric distributions
Network diagram (antecedents → consequents) Top-15 association rule connections
Elbow method (K-Means) Optimal cluster count selection

🛠️ Tech Stack

Tool Purpose
pandas / numpy Data wrangling and feature engineering
matplotlib / seaborn Visualisation
plotly Interactive treemap
mlxtend Apriori, FP-Growth, association rules
scikit-learn KNN, KMeans, TruncatedSVD, MultinomialNB, metrics
networkx Association rule network diagram

🚀 How to Run

  1. Download the dataset from Kaggle:

  2. Install dependencies:

pip install pandas numpy matplotlib seaborn plotly mlxtend scikit-learn networkx
  1. Run notebooks in this order:
1. The Final Project with Apriori & Fp-Growth Models (2).ipynb   ← Main pipeline
2. Naive_Bayes_Model_Recommender_System.ipynb                    ← Generates recommendations_df.csv
3. NB Model.ipynb                                                ← Evaluation & scoring

🎬 Tutorial Videos

Video Link
Recommendation Systems Tutorial (Part One) YouTube ↗
Why Naive Bayes is still relevant in 2024? YouTube ↗

📝 Notes & Limitations

  • "Staples" dominates Apriori/FP-Growth itemsets due to its high purchase frequency — the analysis is re-run excluding it to surface meaningful product associations
  • The dataset is imbalanced across markets — some regions have significantly more records than others, which may bias recommendations toward high-volume markets
  • The Naive Bayes model trains one model per user cluster (17 total), which is memory-intensive on large datasets
  • KNN gap-filling assumes similar customers have similar preferences — performance depends on the density of the training set
  • The min_support=0.001 threshold is intentionally low to accommodate the long product name strings in this dataset

👤 Author

Independent research project — designed and built as a dissertation-level exploration of recommendation system architectures using real-world retail data.
YouTube Channel: RonyMLE

About

A full end-to-end recommendation engine built on the Global Superstore dataset, implementing and comparing three algorithms — Apriori, FP-Growth, and Naive Bayes — to generate personalised product recommendations based on customer purchase history.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages