Classification - Customer Repurchase Window Prediction
Predicting customer repurchase behavior and timing using historical transaction data from an online retail business.
Dataset Source: Online Retail II UCI Dataset Problem Type: Classification Target Variable: Customer repurchase probability within specific time windows Use Case: Customer retention strategies, inventory management, targeted marketing campaigns
Package Imports
Xplainable Cloud Setup
Data Loading and Exploration
Load the Online Retail II dataset and perform basic data exploration.
| Invoice | StockCode | Description | Quantity | InvoiceDate | Price | Customer ID | Country | Amount | |
|---|---|---|---|---|---|---|---|---|---|
| 0 | 489434 | 85048 | 15CM CHRISTMAS GLASS BALL 20 LIGHTS | 12 | 2009-12-01 07:45:00 | 6.95 | 13085 | United Kingdom | 83.4 |
| 1 | 489434 | 79323P | PINK CHERRY LIGHTS | 12 | 2009-12-01 07:45:00 | 6.75 | 13085 | United Kingdom | 81 |
| 2 | 489434 | 79323W | WHITE CHERRY LIGHTS | 12 | 2009-12-01 07:45:00 | 6.75 | 13085 | United Kingdom | 81 |
| 3 | 489434 | 22041 | RECORD FRAME 7" SINGLE SIZE | 48 | 2009-12-01 07:45:00 | 2.1 | 13085 | United Kingdom | 100.8 |
| 4 | 489434 | 21232 | STRAWBERRY CERAMIC TRINKET BOX | 24 | 2009-12-01 07:45:00 | 1.25 | 13085 | United Kingdom | 30 |
The timeline below illustrates the core problem the model is solving: will a customer place another order within 30 days of a given purchase? Each row represents an individual customer (C1 – C4), and every blue dot marks one of their historical purchases. From each purchase, a magenta line extends 30 days—the evaluation window used to create the training label. When a follow-up order actually arrives inside that window, it is highlighted with a pink star. Purchases followed by a star are the positive cases (“repurchased”), while those without a star are negative. Visually stepping through these tracks makes it clear how the dataset converts raw transactions into a binary outcome that the model can learn to predict.
1. Data Preprocessing
Data Preview and Initial Exploration
| Invoice | StockCode | Description | Quantity | InvoiceDate | Price | Customer ID | Country | Amount | InvoiceMonth | |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 489434 | 85048 | 15CM CHRISTMAS GLASS BALL 20 LIGHTS | 12 | 2009-12-01 07:45:00 | 6.95 | 13085 | United Kingdom | 83.4 | 2009-12 |
| 1 | 489434 | 79323P | PINK CHERRY LIGHTS | 12 | 2009-12-01 07:45:00 | 6.75 | 13085 | United Kingdom | 81 | 2009-12 |
| 2 | 489434 | 79323W | WHITE CHERRY LIGHTS | 12 | 2009-12-01 07:45:00 | 6.75 | 13085 | United Kingdom | 81 | 2009-12 |
| 3 | 489434 | 22041 | RECORD FRAME 7" SINGLE SIZE | 48 | 2009-12-01 07:45:00 | 2.1 | 13085 | United Kingdom | 100.8 | 2009-12 |
| 4 | 489434 | 21232 | STRAWBERRY CERAMIC TRINKET BOX | 24 | 2009-12-01 07:45:00 | 1.25 | 13085 | United Kingdom | 30 | 2009-12 |
RFM Feature Engineering
Build 30-day Repurchase Label
Train/Test Time-based Split
| Frequency | DistinctItems | Monetary | Country | Recency | Month | Quarter | |
|---|---|---|---|---|---|---|---|
| 0 | 5 | 26 | 113.50 | United Kingdom | 12.0 | 12 | 4 |
| 1 | 4 | 20 | 90.00 | United Kingdom | 16.0 | 1 | 1 |
| 2 | 1 | 5 | 27.05 | United Kingdom | 28.0 | 3 | 1 |
| 3 | 1 | 19 | 142.31 | United Kingdom | 1.0 | 6 | 2 |
| 4 | 1 | 74215 | 77183.60 | United Kingdom | 216.0 | 1 | 1 |
| ... | ... | ... | ... | ... | ... | ... | ... |
| 25589 | 1 | 494 | 833.48 | United Kingdom | 10.0 | 8 | 3 |
| 25590 | 1 | 732 | 1071.61 | United Kingdom | 13.0 | 5 | 2 |
| 25591 | 2 | 508 | 892.60 | United Kingdom | 8.0 | 9 | 3 |
| 25592 | 1 | 187 | 381.50 | United Kingdom | 7.0 | 11 | 4 |
| 25593 | 1 | 488 | 765.28 | United Kingdom | 8.0 | 5 | 2 |