← All projects

Personal project

Retail purchase prediction

A Spark pipeline on real online-retail transactions that segments customers and predicts who will buy in a given category, evaluated honestly on future months rather than a random split.

Grouped bar chart of lift in the top 10% of ranked customers, for three targets, the order-count rule against boosted trees. Any purchase 1.84 then 1.93, kitchen 1.94 then 2.17, bags 2.28 then 3.03, where random targeting is 1.0.
Targeting the top-ranked tenth of customers finds up to three times as many buyers as random, most of all for the rarest category.

Summary

  • A PySpark pipeline on Databricks over real online-retail transactions: feature engineering from each customer's purchase history, k-means customer segmentation, and per-category purchase-propensity models.
  • Evaluated with a rolling time split, training on earlier months and testing on later ones, so the scores reflect future performance rather than leakage from a random split.
  • Gradient-boosted trees raised a category's average precision from 0.50 to 0.69 over the baseline.
  • Experiments tracked with MLflow, and the pipeline was also run once on Azure Databricks with the infrastructure provisioned by Terraform.

Overview

This project predicts, from a customer's past behaviour, how likely they are to buy within a product category in the coming period. On an imbalanced retail dataset, where most customers do not buy any given category, I treated it as a ranking problem and measured average precision rather than accuracy, which would be misleading when the positive class is rare.

Data and features

The input is a set of real online-retail transactions. From each customer's purchase history I built features summarising their recent and overall activity, aggregated at scale with Spark so the same pipeline runs on the full dataset rather than a sample. Customers were grouped with k-means segmentation to describe the main behavioural segments in the data.

Modelling and evaluation

Per-category propensity models were trained to rank customers by their likelihood to buy. The key choice was the evaluation protocol: a rolling time split that trains on earlier months and tests on later ones, which mirrors how the model would be used in practice and avoids the optimism of a random train and test split on time-ordered data. Against the baseline, gradient-boosted trees raised a category's average precision from 0.50 to 0.69. Runs and metrics were logged with MLflow.

Infrastructure

The pipeline was developed on Databricks and also run once on Azure Databricks, with the cloud resources defined in Terraform so the environment could be created and torn down reproducibly rather than clicked together by hand.