Interactive guide

Flagging fraud, without fooling yourself about how well it works

25 credit card transactions, 5 of them fraudulent. Before any model gets built, part of this data is set aside and never touched again until the very end. Cross-validation is what happens to the rest, and fraud, being rare, is exactly the case where doing it carelessly goes wrong.

Two separate questions, two separate sets of data

"Which settings work best?" gets answered using a training set, repeatedly, via cross-validation. "How well does the finished model actually work?" gets answered exactly once, using a test set that was never involved in choosing anything. Mixing the two up, even by accident, makes a model look better than it really is.

The model here is deliberately the simplest possible: a single dollar-amount threshold: flag anything above it as fraud. That's on purpose. The model isn't the point; the evaluation procedure is.

Fraud is also rare: only 5 of these 25 transactions. That scarcity is exactly what makes plain, careless shuffling dangerous, as the last section below shows directly.

The full procedure

  1. 1

    Hold out a test set first

    Set aside before anything else happens; it won't be touched again until the final step.

  2. 2

    K-fold CV on what's left

    Split the training set into K folds, rotate which one validates, average the K scores.

  3. 3

    Retrain on the full training set

    Once you're confident in the approach, train one final model on all of it, no folds this time.

  4. 4

    Test once, on the held-out set

    The only honest measure of real-world performance, because it's the only data the model never influenced.

All 25 transactions, split once, before any modeling happens.

All Transactions: 25
Training Set: 20
Test Set: 5 (held out)
Fold 1
Fold 2
Fold 3
Fold 4
Fold 5

The Test Set (right) sits outside this whole diagram from here on; it comes back only in "Final evaluation" below.

Step through each round: one fold validates (green), the rest train (blue).

Round 1 of 5

This round's accuracy
This round's fraud recall
Average across all 5 rounds

Every training transaction, this round: which fold it's in, and what this round's threshold predicts for it.

# Amount Actual Fold Role this round Predicted Result

Final evaluation: the Test Set, touched for the first and only time.

Train on all 20 (no folds this time)
Test Set: 5
Final test accuracy
Final fraud recall

The jargons

Every term below exists to answer one question honestly — how will this model perform on data it hasn’t seen — and each one names a specific way that honesty can quietly get compromised.

Test set

Held out before any modeling begins, and touched exactly once: the only honest read on real-world performance.

Fold

One chunk of the training set. K-fold CV rotates which fold validates while the rest train.

Fraud recall

Of the actual fraud cases, what fraction got caught. A fold with zero fraud cases can't report this at all; it's undefined, not zero.

Class imbalance

When one label is much rarer than another: fraud detection, disease screening, and spam are classic examples.

Stratified K-fold

Builds each fold to match the whole dataset's class proportions, instead of leaving it to chance.

Data leakage

Letting information from validation or test data influence training, even accidentally. It makes evaluation look better than it really is.

When classes are imbalanced, plain shuffling isn't enough

Same 25 transactions, same 5 folds, but now compare how the 5 fraud cases land depending on whether the shuffle knows about class labels at all.

Regular K-fold: fraud lands wherever it happens to land

Stratified K-fold: every fold gets its fair share

 fraud cases in this fold    legitimate cases in this fold

A teaching tool: 25 fixed transactions, split with a fixed seed, so every fold, threshold, and recall here is exactly reproducible.