biplobgon/product-recommendation-system
๐๏ธ Product Recommendation System
<!--โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ PART 1 โ PRODUCT LINKS โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ-->
๐ Live Demos
Run locally in two commands: ``bash uvicorn src.app.api:app --host 0.0.0.0 --port 8000 --reload streamlit run src/app/dashboard.py --server.port 8501 ``<!--โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ PART 2 โ EXECUTIVE SUMMARY โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ-->
๐ Overview
End-to-end Product Recommendation System built on the real-world RetailRocket e-commerce dataset (~2.75 M user interaction events). The project covers the full ML lifecycle โ EDA โ feature engineering โ model training โ offline evaluation โ REST API + interactive dashboard โ using a 4-model hybrid architecture that handles both cold-start and warm-start users.
Core prediction goal: Given a visitor's browsing session and historical interactions, predict the next N products they are most likely to purchase.
๐ฏ Executive Summary
The Problem
E-commerce recommendation engines face a brutal sparsity problem. In this dataset, >70 % of visitors have โค 3 interactions and transactions represent only 0.5 % of all events. A single model cannot solve this:
- Pure Collaborative Filtering fails for cold-start users (most of them).
- Pure Content-Based filtering ignores rich co-purchase signals.
- Pure Session-Based models lose long-term preference memory.
The Solution
A hybrid stack of four complementary models working together โ each covering the blind spots of the others:
Results at a Glance
ALS and Content-Based register 0.0 on this evaluation because the test set is constructed from the most recent sessions โ users in that window are not present in training (temporal cold-start). This is expected and realistic. The session model โ which requires no user history โ is the strongest performer.
Why This Matters for a Business
- Personalisation from session 1 โ session-based model requires zero user history.
- Full catalogue coverage โ content-based ensures every item (even those with no purchase history) can surface.
- Modular, swappable stack โ each model is independently loadable and overridable via API parameter.
- Sub-100 ms serving โ all models are loaded in-memory; no database joins at request time.
<!--โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ PART 3 โ TECHNICAL DEEP DIVE โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ-->
๐ฌ Technical Deep Dive
๐ Dataset
Source: RetailRocket E-commerce Dataset (JunโSep 2015)
Interaction signal weights used in training: view = 1 ยท addtocart = 5 ยท transaction = 10
๐ง System Architecture
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Incoming Request โ
โ visitor_id ยท session_items ยท top_k โ
โโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โผ โผ โผ
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ
โ ALS (CF) โ โ TF-IDF (CB) โ โ Item-KNN (SB) โ
โ weight=0.4 โ โ weight=0.2 โ โ weight=0.4 โ
โ warm users โ โ cold items โ โ all visitors โ
โโโโโโโโฌโโโโโโโ โโโโโโโโฌโโโโโโโโโ โโโโโโโโโโฌโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโ
โ Weighted Blend โ
โ + min-max norm โ
โโโโโโโโโโฌโโโโโโโโโโ
โผ
Top-K RecommendationsSession boundary: 1-hour inactivity gap ยท Max sequence length: 20 items ยท Temporal train/test split: leave-last-session-out
๐๏ธ Model Implementations
ALS โ Collaborative Filtering (src/models/collaborative_filtering.py)
- Pure numpy / scipy implementation of the Hu-Koren-Volinsky (2008) Alternating Least Squares algorithm โ no external library dependency (the
implicitpackage doesn't support Python 3.14). - Weighted implicit feedback matrix with
factors=32, iterations=10, regularisation=0.01. - Trained on 31,880 warm users ร 344,728 weighted interactions.
Content-Based โ TF-IDF (src/models/content_based.py)
- Scikit-learn
TfidfVectorizerover concatenated item property strings (categoryid + price bucket + availability). max_features=500to keep the matrix (417k ร 500) tractable.- Cosine similarity at query time; no pre-computed pairwise matrix (memory efficient).
Session-Based โ Item-KNN (src/models/session_based.py)
- Co-occurrence matrix over 386,099 training sequences, recency-weighted (more recent sessions count more).
- No PyTorch dependency โ pure numpy co-occurrence counting with exponential recency decay.
- At query time: sums co-occurrence scores for all items in the current session; returns top-K.
Hybrid (src/models/hybrid.py)
- Calls all three models, normalises scores to [0, 1], applies configurable weights, and merges.
- Gracefully degrades: if ALS has no embedding for a new user, only CB + Session contribute.
โ๏ธ Feature Engineering
Generated by running notebooks/02_feature_engineering.ipynb.
๐ Evaluation Metrics
Results saved to outputs/reports/evaluation_report.csv. Run evaluation independently via:
python src/training/run_evaluation.py๐ API Reference
uvicorn src.app.api:app --host 0.0.0.0 --port 8000 --reloadExample:
curl "http://localhost:8000/recommend/12345?session_items=101,202&top_k=10"{
"visitor_id": 12345,
"recommended_items": [876, 205, 934, 412, 778, 66, 543, 188, 301, 407],
"model": "hybrid",
"latency_ms": 38
}๐ Project Structure
product-recommendation-system/
โ
โโโ data/
โ โโโ raw/ # Original RetailRocket CSVs
โ โ โโโ events.csv
โ โ โโโ category_tree.csv
โ โ โโโ item_properties_part1.csv
โ โ โโโ item_properties_part2.csv
โ โโโ processed/ # Generated feature files (gitignored โ too large)
โ โโโ user_features.csv # 1,407,580 users ร 12 features
โ โโโ item_features.csv # 417,053 items ร 3 features
โ โโโ interactions.csv # 2,145,179 user-item weighted interactions
โ โโโ session_features.csv # 1,726,714 sessions ร 9 features
โ โโโ session_sequences.csv # 386,099 training sequences
โ โโโ tfidf_matrix.npz # Sparse TF-IDF (417k ร 500)
โ โโโ tfidf_item_ids.csv # Item ID โ TF-IDF row index mapping
โ โโโ tfidf_vectorizer.pkl # Fitted sklearn TfidfVectorizer
โ
โโโ notebooks/
โ โโโ eda.ipynb # 01 โ Full EDA (25 cells, 25 visualisations)
โ โโโ 02_feature_engineering.ipynb # 02 โ User / item / session feature generation
โ โโโ 03_model_training.ipynb # 03 โ Train ALS, CB, Session, Hybrid
โ โโโ 04_model_evaluation.ipynb # 04 โ Offline metrics + comparison charts
โ
โโโ src/
โ โโโ utils/
โ โ โโโ config.py # YAML loader with dot-notation access
โ โ โโโ logger.py # Centralised logging
โ โ
โ โโโ features/
โ โ โโโ user_features.py # Visitor-level aggregates
โ โ โโโ item_features.py # Item metadata + price/availability parsing
โ โ โโโ session_features.py # Session segmentation + sequence building
โ โ
โ โโโ models/
โ โ โโโ collaborative_filtering.py # ALSRecommender (pure numpy/scipy)
โ โ โโโ content_based.py # ContentBasedRecommender (sklearn TF-IDF)
โ โ โโโ session_based.py # SessionBasedRecommender (Item-KNN, numpy)
โ โ โโโ hybrid.py # HybridRecommender (weighted blend)
โ โ
โ โโโ training/
โ โ โโโ train.py # Full end-to-end training pipeline
โ โ โโโ resume_training.py # Skip CB re-training; reload saved ALS+CB
โ โ โโโ run_evaluation.py # Evaluate all models โ evaluation_report.csv
โ โ โโโ evaluate.py # HR@K, NDCG@K, MRR@K, Coverage, Novelty
โ โ
โ โโโ app/
โ โ โโโ api.py # FastAPI service (5 endpoints)
โ โ โโโ dashboard.py # Streamlit dashboard (3 tabs)
โ โ
โ โโโ data_prep.py # Raw data cleaning & validation
โ โโโ create_sample.py # Stratified sampling for fast iteration
โ โโโ gcs_loader.py # Download raw files from Google Cloud Storage
โ
โโโ outputs/
โ โโโ models/ # Serialised PKL artefacts (gitignored โ large)
โ โ โโโ als_model.pkl # 17.6 MB
โ โ โโโ content_based_model.pkl # 338 MB
โ โ โโโ session_based_model.pkl # 14.1 MB
โ โ โโโ hybrid_model.pkl # 370 MB
โ โโโ reports/
โ โโโ evaluation_report.csv # All model metrics across K=5,10
โ
โโโ configs/
โ โโโ model_config.yaml # Hyperparameters & data paths
โ โโโ pipeline_config.yaml # Training pipeline, MLflow, retrain schedule
โ
โโโ assets/ # Static images for README
โโโ .gitignore
โโโ requirements.txt
โโโ README.md๐ End-to-End Pipeline
Raw CSVs โ EDA (01) โ Feature Engineering (02)
โ
โโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโ
โ data/processed/ โ
โ user_features ยท item_features โ
โ tfidf_matrix ยท interactions โ
โ session_sequences โ
โโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโ
โ Training (train.py) โ
โ ALS โ CB (40 min) โ Session โ
โ โ Hybrid โ
โโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโ
โ Evaluation (run_evaluation.py) โ
โ HR@K ยท NDCG@K ยท MRR@K โ
โ Coverage ยท Novelty โ
โโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโ
โ Serving โ
โ FastAPI :8000 ยท Streamlit :8501โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโQuick start:
# 1. Install dependencies
pip install -r requirements.txt
# 2. Pull raw data (or place CSVs manually in data/raw/)
cp .env.example .env # set GCS_BUCKET_NAME
python src/gcs_loader.py
# 3. Run feature engineering
jupyter nbconvert --to notebook --execute notebooks/02_feature_engineering.ipynb
# 4. Train models (full run ~50 min; CB is the bottleneck)
python src/training/train.py
# 5. Or, if models are already saved, run a fast retrain (session only, ~30 s)
python src/training/resume_training.py
# 6. Evaluate
python src/training/run_evaluation.py
# 7. Serve
uvicorn src.app.api:app --reload
streamlit run src/app/dashboard.py๐ชฒ Pitfalls Faced & How They Were Solved
๐ Future Enhancements
โ๏ธ Google Cloud Storage Setup
Raw files can be pulled from a GCS bucket automatically.
gcloud auth application-default login
cp .env.example .env # set GCS_BUCKET_NAME
python src/gcs_loader.py๐ค Author
Biplob Gon ยท Data Scientist | AI/ML | Recommender Systems
 
โญ Found this useful?
Give the repo a โญ โ it helps others discover it.
