End-to-End ML Use Cases: Fraud, Recommendations, Forecasting and Ticket Routing
Who it is for
What you will achieve
Prerequisites
Curriculum
- Step 1: Fraud Scoring, Part 1: The Problem, the Metric and the Baseline
Before training a fraud model, write down what good means. On 6,000 synthetic validation transactions, a rule that never flags is 96.13% accurate and costs 23,200, flagging everyone costs 28,840, and a small contract.py fixes the rules first.
- Step 2: Fraud Scoring, Part 2: Training With Time Splits, Leak-Free Features and a Cost-Picked Threshold
Train the fraud model against its contract: a time split of 30,000 synthetic transactions, a card_n_1h feature that never looks ahead, XGBoost that pays 23,200 at 0.5, and a cost-picked threshold of 0.045 that brings validation to 14,955 and test to 16,765.
- Step 3: Fraud Scoring, Part 3: Serving It With FastAPI, Docker and a 50 ms Budget
A payment is waiting on the fraud score. Time each stage (the model takes 0.43 ms of a 3.3 ms request), then load test: p99 is 6.1 ms with one client and 163.3 ms with 32, against an example 50 ms budget. Then a Dockerfile, built and run once.
- Step 4: Fraud Scoring, Part 4: Release and Monitoring With Shadow, Drift and Late Labels
A challenger fraud model runs in shadow beside the champion. Background shadow costs about a millisecond at p50, simulated drift takes PSI from 0.007 to 0.181, and a recall of 1.00 on day 35 turns into 0.41 by day 80 once the chargebacks arrive.
- Step 5: Recommendations, Part 1: Two Stages, Candidates Then Ranking
How does a shop pick ten items out of millions? A cheap score takes 4.83 ms over a million items where a careful one takes 1,246 ms, so stage one narrows 120 items to 30 and stage two orders them. Hit rate: popularity 0.263, two stages 0.537, ceiling 0.807.
- Step 6: Recommendations, Part 2: Candidate Generation With Embeddings
Build stage one of a recommender from nothing but who had what: TruncatedSVD squeezes a 300 by 120 grid into 8 numbers per user and item, one matrix multiply finds 30 candidates (hit rate 0.807 vs 0.657 for popularity), and dim 8 beats both 2 and 32.
- Step 7: Recommendations, Part 3: The Ranker and Offline Metrics (Recall@k and nDCG)
Stage two re-orders 30 candidates with three features and a gradient boosted classifier trained on a second split. Recall@10 rises from 0.523 to 0.537, recall@30 can't move from 0.807, and nDCG@10 slips from 0.282 to 0.275. Here's why.
- Step 8: Recommendations, Part 4: Serving With FastAPI, a Cache and a Popular Fallback
A recommendations endpoint that always has an answer: precomputed candidates, a 60-second cache that hits 0.819 of 2,000 simulated requests, and a popular list for model errors, new users and candidates older than an hour, each answer tagged with its source.
- Step 9: Demand Forecasting, Part 1: The Business Problem, a Price per Mistake and the Line to Beat
Before training a forecast model for a small shop, decide what a wrong forecast costs. On synthetic milk, bread and rice demand, same weekday last week misses by 6.78 units, yesterday's sales by 18.97, and adding 5 units raises the error but cuts the bill from 17,365 to 14,032.
- Step 10: Demand Forecasting, Part 2: Walk-Forward Backtesting, Ridge vs Boosting and the Slices an Average Hides
Walk forward through four 28-day folds of synthetic shop demand. Ridge beats the same-weekday baseline in all 12 product-fold cells (MAE 5.81 vs 6.91), default gradient boosting lands at 6.07, a new product is short for weeks, and a leaky rolling mean fakes a drop to 5.19.
- Step 11: Demand Forecasting, Part 3: The Nightly Batch Pipeline That Survives a Crash
Make the shop's forecast a nightly job, then kill it halfway: a plain rerun publishes 35 rows instead of 21 with exit code 0. A run id from the date, one part file per product and a primary key make the resume print scored 1, skipped 2, published 21.
- Step 12: Demand Forecasting, Part 4: Serving the Forecasts, the Outcome Join and a Retrain Rule
Serve 315 published forecasts through one FastAPI route that never runs the model, then join them to what actually sold. A synthetic milk promo pushes milk's recent-to-older error ratio to 6.61 and the rule flags only milk, but a week earlier the same rule fired on one bad day.
- Step 13: Ticket Routing, Part 1: A Safe Split, Keyword Rules and a TF-IDF Baseline
Before training anything big to route support tickets, set a fair test and a cheap score to beat. On 2,400 synthetic tickets, a random split shows 0.927, a customer-and-time split shows 0.669, keyword rules get 0.550, and TF-IDF falls to 0.436 on tickets that use new words.
- Step 14: Ticket Routing, Part 2: Fine-Tuning a Small Encoder on a CPU, With a Run Log and Honest Ranges
Fine-tune a 22.7-million-weight encoder (all-MiniLM-L6-v2, Apache 2.0) to route synthetic tickets on a CPU in about 48 seconds. It scores 0.887 [0.831, 0.931] against TF-IDF's 0.669, the whole gap comes from tickets with new words (0.885 vs 0.436), and vague tickets stay stuck at 0.286.
- Step 15: Ticket Routing, Part 3: Quantise to int8, Host Behind FastAPI and Measure the Server
Shrink the ticket encoder to int8 (3 of 537 validation tickets change, 90.9 MB becomes 58.6 MB), serve it with FastAPI and measure the server: on this CPU, int8 is slower (p95 24.9 ms vs 14.3 ms) and the process uses more memory (762 MB vs 556 MB).
- Step 16: Ticket Routing, Part 4: Canary, Automatic Rollback and "Does Your Small Model Reason?"
Release the int8 ticket router in stages: 10%, 50%, then everyone, with a floor of stable accuracy minus 0.05: int8 goes through, and a simulated 2-bit model scoring 0.179 on its first 28 tickets is rolled back. Then 24 hand-written probes test the claim that it reasons: 8/8, 5/8, 5/8.