An explainable, production-ready Machine Learning system that predicts Return-to-Origin (RTO) risk on Indian e-commerce orders prior to warehouse dispatch, enabling merchants to mitigate logistics losses while preserving customer trust.
🌐 Live Web Application: https://rto-risk-predictor-gowtham.streamlit.app
In Indian e-commerce, Cash on Delivery (COD) represents over 60% of consumer transactions. However, roughly 1 in 4 COD orders results in a Return-to-Origin (RTO) — the parcel is dispatched, transported across the country, but rejected or uncollected at the customer's doorstep.
When an order returns undelivered:
- The seller absorbs two-way courier freight (~₹80 forward + ~₹80 reverse).
- Packaging materials (~₹20) are permanently damaged.
- Inventory is trapped in transit for 10–14 days, causing stockouts.
- Average direct loss: ₹200 per returned order.
Across the Indian retail ecosystem, RTO amounts to an estimated ₹8,000+ crore annual loss. Crucially, once a parcel is handed to the delivery courier, this operational loss is locked in. Prevention must happen post-order checkout, but pre-dispatch.
-
Predictive Risk Scoring: Calculate a calibrated probability
$P(\text{RTO}) \in [0, 1]$ for every incoming order strictly using attributes knowable at checkout. -
Actionable Merchant Intervention: Categorize orders into actionable operational tiers:
-
🟢 Low Risk (
$P < 35%$ ): 1-click auto-dispatch with zero verification friction. -
🟡 Medium Risk (
$35% \le P < 50%$ ): Automated WhatsApp / SMS address confirmation link. -
🔴 High Risk (
$P \ge 50%$ ): Proactive intervention (requesting a ₹50–₹100 advance deposit or prepaid conversion).
-
🟢 Low Risk (
- Strict Explainability: Restrict models to linear coefficients (Logistic Regression) and transparent splits (Decision Trees) so business operators understand why an order is flagged.
- Zero Data Leakage: Ensure training pipelines use strict chronological splitting and encapsulation.
- Volume: 50,000 orders generated deterministically (seed
20260101). - Real Geographic Skeleton: 23,899 active Indian postal pincodes across 566 districts and 35 states/UTs from the official India Post directory (
data/external/india_pincodes.csv). - Calibrated Marginals: Intercepts solved to reproduce published Indian logistics benchmarks across payment modes, order value tiers, and categories.
Important
Synthetic Calibration Disclosure: Because real e-commerce transactional outcome logs contain sensitive consumer PII and proprietary courier agreements, this dataset is synthetically generated using seeded Bernoulli draws over calibrated log-odds. While marginal return rates and correlations accurately mirror Indian retail benchmarks, external production deployment requires fine-tuning on merchant-specific historical data.
To guarantee zero data leakage, features are restricted strictly to pre-dispatch variables:
| Feature Name | Type | Domain / Values | Causal Relevance |
|---|---|---|---|
payment_mode |
Categorical | COD, PREPAID |
Dominant driver: COD orders have an 18x higher RTO rate (25.6%) than Prepaid (1.4%). |
category |
Categorical | fashion, footwear, accessories, beauty, home_kitchen, electronics |
Fashion exhibits the highest RTO rate (27.7%) due to sizing and fit mismatch. |
pincode_tier |
Categorical | metro, tier_1, tier_2, tier_3 |
Urbanization proxy; RTO increases monotonically from Metro (12.6%) to Tier 3 (26.5%). |
order_value |
Numerical | ₹99 to ₹41,896 | Price magnitude influences buyer financial commitment. |
discount_pct |
Numerical | 0.0% to 71.5% | Steep discounts trigger impulsive buying and buyer remorse upon delivery. |
delivery_days_est |
Numerical | 2.0 to 14.1 days | Transit delays cool buyer enthusiasm and increase cancellation propensity. |
account_age_days |
Numerical | 1 to 570 days | Brand-new accounts present higher baseline cancellation risk than mature accounts. |
order_velocity_24h |
Numerical | 0, 1, 2 | High order frequency in a short window signals erratic checkout sprees. |
is_festive |
Boolean | True, False |
Seasonal holiday sales (e.g. Diwali) elevate courier bottlenecks and returns. |
is_alternate_address |
Boolean | True, False |
Delivery to non-primary locations increases delivery attempt failures. |
- Surrogate IDs & PII:
order_id,customer_id,customer_name,phone,address_line. - High Cardinality Noise: Raw 6-digit
pincode,city,district,state(summarized viapincode_tier). - Temporal Sorting Column:
order_ts(used strictly to order chronological train/val/test splits). - Post-Dispatch Outcomes: Courier delivery attempt status, delivery agent notes, transit tracking timestamps.
Raw Checkout Order
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ Scikit-Learn ColumnTransformer │
│ Categorical Pipeline: SimpleImputer + OneHotEncoder(ignore) │
│ Numerical Pipeline: SimpleImputer(median) + StandardScaler() │
└─────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ Balanced Logistic Regression Pipeline (L-BFGS, C=1.0) │
│ w1 = N / (2 * N1) ≈ 2.95 | w0 = N / (2 * N0) ≈ 0.60 │
└─────────────────────────────────────────────────────────────────┘
│
▼
Calibrated Probability P(RTO)
│
├─► P < 0.35 ──► 🟢 Low Risk (Frictionless Auto-Dispatch)
├─► 0.35 <= P < 0.50 ──► 🟡 Medium Risk (Automated WhatsApp Verification)
└─► P >= 0.50 ──► 🔴 High Risk (Mandatory Advance Deposit)
Instead of random splitting (which leaks future events into past training data), orders are split strictly along order_ts:
- Training Set (70%): 35,000 orders (Jan 1, 2024 to Jan 22, 2025)
- Validation Set (10%): 5,000 orders (Jan 22, 2025 to Mar 16, 2025) — for model & threshold selection
- Held-Out Test Set (20%): 10,000 orders (Mar 16, 2025 to Jun 30, 2025) — evaluated once at final verification
| Model Architecture | Objective / Configuration | Key Result |
|---|---|---|
| 1. Baseline Logistic Regression | Unweighted loss (solver='lbfgs') |
Flawed. Optimizes accuracy (84.5%) by predicting "Delivered" for almost all orders, catching only 2.7% of returns. |
| 2. Balanced Logistic Regression |
class_weight='balanced' ( |
⭐ Selected Winner. Balances precision and recall, catching 75.4% of RTOs on validation and 72.4% on held-out test data. |
| 3. Decision Tree Classifier | max_depth=5, class_weight='balanced' |
Provides readable if-then rules, but achieves lower ROC-AUC (0.7813) due to discrete step boundaries. |
| Model | Accuracy | Precision | Recall | F1-Score | ROC-AUC | RTO Caught (TP) | Missed (FN) |
|---|---|---|---|---|---|---|---|
| Baseline LR (Unweighted) | 84.46% | 47.73% | 2.71% | 0.0513 | 0.7994 | 21 | 754 |
| Balanced LR (Winning) | 68.98% | 30.04% | 75.35% | 0.4296 | 0.7993 | 584 | 191 |
| Balanced Decision Tree | 68.98% | 29.25% | 70.58% | 0.4136 | 0.7813 | 547 | 228 |
precision recall f1-score support
Delivered (0) 0.9366 0.7051 0.8045 8527
RTO (1) 0.2977 0.7237 0.4218 1473
accuracy 0.7078 10000
macro avg 0.6171 0.7144 0.6132 10000
weighted avg 0.8425 0.7078 0.7481 10000
-
Test ROC-AUC: 0.7924 (vs. 0.7993 on validation — minimal drop of
$-0.0069$ , verifying robust generalization) - Test Recall: 72.37% (catches 1,066 out of 1,473 RTOs)
-
Financial Impact on 10,000 Orders:
- Unmitigated Logistics Loss: ₹2,94,600
- Net Model Operating Loss: ₹1,31,700
- Net Merchant Savings: ₹1,62,900 (55.3% reduction in RTO waste)
- macOS / Linux / Windows
- Python 3.9 or higher
git clone https://github.com/Gowtham10844/RTO-Risk-Predictor.git
cd RTO-Risk-Predictor
# Create and activate virtual environment
python3 -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install dependencies
pip install -r requirements.txtpython notebooks/exploration.pypython src/train_model.pyOutputs model comparison metrics, threshold sweeps, and serializes the winning pipeline to models/rto_model.joblib.
python src/evaluate_test.pystreamlit run app.pyOpens the interactive dashboard at http://localhost:8501.
The Streamlit dashboard (app.py) provides three operational modes:
-
Single Order Risk Scorer:
- Real-time parameter inputs (payment mode, order value, category, tier, discount, transit ETA, account age).
- One-click testing presets (High Risk COD, Safe Prepaid, Borderline Festive).
- Instant calculation of
$P(\text{RTO})$ , Expected Logistics Waste (₹), and Action Badges.
-
Model & Economics Benchmark:
- Full side-by-side metric tables across baseline, balanced logistic regression, and decision trees.
- P&L financial simulation showing rupee savings per 5,000 orders.
-
Live Batch Validation Sample:
- Interactive inference engine sampling real unseen validation records from
data/processed/val.parquet.
- Interactive inference engine sampling real unseen validation records from
- Address Text Embeddings: The current model excludes raw address strings to prevent memorization. Future versions can incorporate lightweight transformer embeddings (or regex-extracted street clarity metrics) to capture vague addresses without PII risk.
-
Dynamic Risk Thresholds: The decision threshold is currently static (
$0.50$ ). In production, thresholds can dynamically adjust based on gross margin: high-margin products can tolerate higher RTO risk, while thin-margin goods require stricter cutoffs. - Real-time Merchant Feedback Loop: Incorporating courier delivery feedback to update customer reliability priors online.
RTO-Risk-Predictor/
├── .gitignore # Git exclusion specifications
├── README.md # Project documentation
├── PROGRESS.md # 5-Day roadmap checklist (20/20 complete)
├── DAY1_NOTES.md # Dataset schema & ML fundamentals
├── DAY2_NOTES.md # Data preprocessing & baseline pipeline
├── DAY3_EVALUATION.md # Model comparison & threshold analysis
├── INTERVIEW_PREP.md # 2-minute pitch & 10 interview Q&As
├── requirements.txt # Frozen Python dependencies
├── app.py # Streamlit web dashboard
├── config/
│ └── evidence.yaml # Indian e-commerce benchmark calibration parameters
├── data/
│ ├── external/ # India Post official pincode directory
│ ├── raw/ # Raw 50,000 orders CSV
│ └── processed/ # Chronological train, val, and test parquet splits
├── models/
│ └── rto_model.joblib # Serialized winning pipeline
├── notebooks/
│ ├── exploration.ipynb # Jupyter exploration notebook with outputs
│ └── exploration.py # Standalone exploration script
└── src/
├── __init__.py
├── features.py # Feature extraction & encoding logic
├── generate.py # Synthetic dataset generation engine
├── train_model.py # Model training, comparison & serialization
├── predict.py # Inference & risk assessment helper
└── evaluate_test.py # Final test set evaluation script