A production-grade data science project demonstrating end-to-end customer analytics, statistical hypothesis testing, and actionable business intelligence.
Executive Summary β’ Key Findings β’ Statistical Analysis β’ Visuals β’ How to Run
ecommerce-rfm-analysis/
β
βββ src/
β βββ analysis.py # Full modular pipeline (PEP 8 compliant)
β
βββ visuals/ # All generated charts (PNG, 150 dpi)
β βββ 01_segment_distribution.png
β βββ 02_rfm_distributions.png
β βββ 03_monetary_by_segment.png
β βββ 04_rfm_scatter.png
β βββ 05_monthly_revenue.png
β βββ 06_correlation_heatmap.png
β βββ 07_confidence_intervals.png
β
βββ reports/
β βββ executive_report.txt # Full statistical report
β
βββ requirements.txt
βββ README.md
Problem: An e-commerce business lacks clarity on which customers drive revenue, which are churning, and where marketing spend should be prioritized.
Solution: This project applies RFM (Recency, Frequency, Monetary) segmentation combined with rigorous statistical testing to quantify customer value, validate segment differences, and generate five data-backed business recommendations.
Dataset Overview:
- ~50,000 raw transactions spanning 2 years (2022β2023)
- 1,200 unique customers across 5 countries
- Intentional data quality issues: duplicates, missing IDs, cancelled orders, price outliers
- Final clean dataset retention: ~87% after robust cleaning
Stack: Python Β· Pandas Β· NumPy Β· SciPy Β· Matplotlib Β· Seaborn
| Segment | Customers | Avg Spend | Share of Revenue |
|---|---|---|---|
| π Champions | ~198 | $18,400+ | ~30% |
| π Loyal Customers | ~210 | $14,200+ | ~25% |
| β‘ Potential Loyalists | ~180 | $13,500+ | ~20% |
| ~195 | $11,800+ | ~18% | |
| π€ Lost | ~120 | $7,200+ | ~7% |
| Metric | Value | Interpretation |
|---|---|---|
| Mean Monetary Value | $13,167 | Average lifetime spend per customer |
| Coefficient of Variation | 27.6% | Moderate spend dispersion β pricing opportunity |
| Skewness | +0.40 | Right-skewed β high-value outliers pull mean up |
| Kurtosis | +0.12 | Near-normal tails β few extreme spenders |
| 95% CI for Mean | [$12,961 β $13,372] | Tight CI confirms reliable estimate |
| Test | Result | Conclusion |
|---|---|---|
| Welch's t-test: Champions vs Loyal | t=14.89, pβ0.00 | β Champions spend significantly more |
| One-Way ANOVA: All Segments | F=129.53, pβ0.00 | β Segment means are statistically distinct |
All charts use a dark-theme professional design system for maximum impact in portfolio presentations and client decks.
Who are our customers?
How are Recency, Frequency, and Monetary value distributed?
Do segments actually differ in spending behavior?
Where does each customer sit in the RΓFΓM space?
How does revenue evolve over time?
How do RFM dimensions relate to each other?
How certain are we about segment mean spend?
RFM is a behavioral segmentation framework proven in direct marketing since the 1990s. It captures three independent dimensions of customer value:
- Recency β customers who bought recently are more likely to buy again
- Frequency β customers who buy often signal brand loyalty
- Monetary β customers who spend more have higher LTV (Lifetime Value)
Each dimension is scored 1β5 using quintile binning:
rfm["r_score"] = pd.qcut(rfm["recency"], 5, labels=[5, 4, 3, 2, 1])
rfm["f_score"] = pd.qcut(rfm["frequency"].rank(method="first"), 5, labels=[1,2,3,4,5])
rfm["m_score"] = pd.qcut(rfm["monetary"].rank(method="first"), 5, labels=[1,2,3,4,5])Note: Recency is scored inversely (lower recency = higher score).
Rank-based binning is used for Frequency and Monetary to handle ties robustly.
| Metric | Value | What it means |
|---|---|---|
| Skewness = +0.40 | Moderate right skew | A minority of customers generate outsized revenue β prioritize Champions |
| Kurtosis = +0.12 | Near-mesokurtic | Spend distribution is stable β no extreme tail events distorting averages |
Based on the statistical analysis, five high-priority interventions are recommended:
Champions account for ~30% of total revenue. Action: Launch an invitation-only loyalty tier with exclusive early access, dedicated account manager, and quarterly gifting. Projection: 15β20% churn reduction β $12Kβ18K in protected annual revenue.
~195 customers show declining recency with historically strong frequency. Action: Deploy personalized email with 20% "We Miss You" discount within 7 days of inactivity threshold. Projection: 25% reactivation Γ $180 avg order β +$8,775 in recovered revenue.
~120 customers are fully churned (low R, F, M). Action: A/B test SMS vs. email win-back. Segment by original product category for hyper-relevant offers. Projection: Identify the $4β$6 CAC channel with highest ROI for future re-engagement budget.
This segment buys well per order but infrequently. Action: Introduce a tiered subscription prompt ("Buy 3 times this quarter, unlock 10% off forever"). Projection: +0.8 orders/year per customer across 180 users β +$22,000 incremental annual revenue.
CV of 27.6% signals segment-level price sensitivity differences. Action: Audit top-10 revenue SKUs. Apply dynamic pricing (+5%) for Champions and Loyal segments. Projection: 5% price lift on $180K of inelastic SKU revenue β +$9,000 gross margin improvement.
Python 3.10+# 1. Clone the repository
git clone https://github.com/thed700/ecommerce-rfm-analysis.git
cd ecommerce-rfm-analysis
# 2. Create a virtual environment (recommended)
python -m venv venv
source venv/bin/activate # macOS/Linux
venv\Scripts\activate # Windows
# 3. Install dependencies
pip install -r requirements.txtpython src/analysis.pyThis will:
- Generate realistic synthetic e-commerce data with intentional data quality issues
- Clean and validate all records (with full audit trail)
- Engineer RFM features and assign segment labels
- Compute descriptive statistics, skewness, kurtosis, CV, and confidence intervals
- Run Welch's t-test and One-Way ANOVA
- Output 7 publication-quality charts to
visuals/ - Write the full executive report to
reports/executive_report.txt
pandas>=2.0
numpy>=1.26
matplotlib>=3.8
seaborn>=0.13
scipy>=1.12
- β PEP 8 compliant throughout
- β Fully modular β each pipeline stage is an independent function
- β Type hints on all public functions
- β Docstrings on every function (Google style)
- β
Reproducible β fixed
random_state/seedthroughout - β No data leakage β RFM scoring computed on clean data only
Akmal β Data Analytics & Business Intelligence
π GitHub: @thed700
π Specialization: Statistical Modeling Β· Python Β· SQL Β· Business Intelligence
MIT LICENSE β free to use, adapt, and build upon with attribution.






