An end-to-end unsupervised machine learning pipeline comparing K-Means Clustering and Agglomerative Hierarchical Clustering on online retail transactions (~1M records) using RFM (Recency, Frequency, Monetary) feature analysis.
Targeted marketing requires understanding unique customer behaviors rather than treating all shoppers identically. Transactional data consists of individual purchases, which cannot be fed directly into clustering algorithms without aggregation.
This project engineers Recency, Frequency, and Monetary (RFM) features at the customer level, normalizes features via Log Transformation and StandardScaler, and compares centroid-based (K-Means) versus connectivity-based (Hierarchical) algorithms to build actionable business personas.
- Source: Online Retail II Dataset on Kaggle
- Scale: ~1,000,000 raw invoice transactions.
- Engineered Units: Unique customer-level RFM profiles:
- Recency (R): Days since last completed transaction.
- Frequency (F): Count of distinct completed purchases.
- Monetary (M): Total monetary revenue generated per customer.
- Cleaning: Dropped rows missing
Customer IDand removed negative/cancelled quantities (Quantity > 0). - Aggregation: Calculated total spend per line item (
Quantity * Price) and aggregated metrics grouped byCustomer ID. - Log Transformation: Applied
np.log1p()to handle heavy right-skewness across monetary and frequency distributions. - Feature Scaling: Applied
StandardScalerto normalize feature vectors before distance-based evaluation.
Using ward linkage and Euclidean distances, the tree cut visually confirms 4 distinct natural groupings.
The Within-Cluster Sum of Squares (WCSS) plot displays a distinct elbow point at
Both models were evaluated on scaled features using quantitative internal cluster validation metrics:
| Clustering Algorithm | Silhouette Score ↑ | Davies-Bouldin Index ↓ | Evaluation |
|---|---|---|---|
| K-Means Clustering | 0.3663 | 0.9355 | Slightly higher separation and cluster cohesion. |
| Hierarchical Clustering | 0.3314 | 0.9317 | Comparable performance with slightly better DB index. |
Side-by-side scatter plots illustrating customer distribution across Recency vs. Monetary space:
- VIP / High Spenders (High Monetary, High Recency):
- Strategy: Exclusive VIP rewards, early access to new collections, and dedicated account support.
- Loyal Regulars (Moderate Monetary, Frequent Purchases):
- Strategy: Cross-selling recommendations and loyalty points program to maximize Customer Lifetime Value (CLV).
- Recent / New Buyers (Low Monetary, High Recency):
- Strategy: Welcome discount codes, onboarding email sequences, and popular product suggestions.
- At-Risk / Lost Customers (Low-to-Moderate Monetary, Low Recency):
- Strategy: Targeted win-back email campaigns and re-engagement promotional incentives.
Based on RFM scoring and unsupervised clustering, customers were categorized into actionable segments:
| Cluster / Segment | Avg Recency (Days) | Avg Frequency (Orders) | Avg Monetary ($) | Recommended Marketing Strategy |
|---|---|---|---|---|
| Champions / Best Customers | Low (< 30) | High (> 15) | High (>$10,000) | VIP rewards, early access to new product releases. |
| Loyal Customers | Moderate (30–90) | High (> 10) | Moderate-High | Upsell higher-value products, loyalty programs. |
| At Risk / Need Attention | High (> 120) | Moderate | Moderate | Win-back promotional offers, targeted discounts. |
| Lost Customers | Very High (> 250) | Low (1–2) | Low | Low-cost automated email campaigns. |
Khaled Amireh


