Library · Validation

The placebo test: shuffled labels

Shuffle the target, rerun everything, and look at the score. With no relation left between inputs and target, an honest pipeline scores at chance level. If it scores better, the pipeline is reading the answer somewhere.

By , published

Definition

The placebo keeps the inputs, the code and the evaluation, and breaks the only thing a model is supposed to learn: the link between the inputs and the target. The target column is permuted at random. Repeated many times, this is the permutation test of Ojala and Garriga, and the scores obtained form the distribution of what the pipeline produces when there is nothing to find.

The test is run on the full procedure: feature construction, selection, tuning and scoring. A placebo applied to the last step only would miss the leaks that sit upstream.

Worked example

Two pipelines are given real data with a weak signal, then the same data with shuffled labels. One of them selects features before cross-validation.

import numpy as np
from sklearn.feature_selection import SelectKBest, f_classif
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score
from sklearn.pipeline import make_pipeline

rng = np.random.default_rng(8)
X = rng.normal(size=(150, 2000))
y = (X[:, 0] + rng.normal(scale=2.0, size=150) > 0).astype(int)     # one weakly useful feature

def leaky(X, y):
    Xs = SelectKBest(f_classif, k=15).fit_transform(X, y)           # sees the test folds
    return cross_val_score(LogisticRegression(), Xs, y, cv=5, scoring="roc_auc").mean()

def clean(X, y):
    pipe = make_pipeline(SelectKBest(f_classif, k=15), LogisticRegression())
    return cross_val_score(pipe, X, y, cv=5, scoring="roc_auc").mean()

for name, f in [("selection outside the folds", leaky), ("selection inside the folds", clean)]:
    placebo = [f(X, rng.permutation(y)) for _ in range(10)]
    print(f"{name:28s} AUC on real labels = {f(X, y):.2f}   mean AUC on shuffled labels = {np.mean(placebo):.2f}")

Output, produced by running the code above when this page was built:

selection outside the folds  AUC on real labels = 0.88   mean AUC on shuffled labels = 0.86
selection inside the folds   AUC on real labels = 0.58   mean AUC on shuffled labels = 0.47

On real labels the first pipeline looks much better than the second. The placebo shows why: it scores far above 0.5 on labels that carry no information. The second pipeline returns to chance level on the placebo, and its lower score on real labels is the honest one.

Where it misleads

  • Shuffling destroys the structure of the target as well as its link with the inputs. On time series, a plain permutation removes autocorrelation; shuffle blocks, or shift the target in time, to keep it.
  • The placebo detects leaks that pass through the target. A feature that contains the outcome stays with the inputs when the labels are shuffled, and goes unnoticed. That case needs a review of where each feature comes from.
  • Passing the placebo does not prove the model is good. It only says the pipeline does not manufacture a score from nothing.
  • A few runs are not enough to see a small leak. The spread of the placebo scores gives the resolution of the test.

The placebo has a mirror image, the positive control: plant a known effect and check that the pipeline finds it.

In our work

A placebo is one of the three development checks every result of ours has to pass, with a paired gain and a planted effect. In our credit validation report, 20 placebo models reached a mean AUC of 0.496.

References

  1. Ojala, M., Garriga, G. C. (2010). Permutation tests for studying classifier performance. Journal of Machine Learning Research, 11, 1833-1863. www.jmlr.org/papers/v11/ojala10a.html
  2. Kapoor, S., Narayanan, A. (2023). Leakage and the reproducibility crisis in machine-learning-based science. Patterns, 4(9), 100804. doi:10.1016/j.patter.2023.100804
  3. Kaufman, S., Rosset, S., Perlich, C., Stitelman, O. (2012). Leakage in data mining: formulation, detection, and avoidance. ACM Transactions on Knowledge Discovery from Data, 6(4), 1-21. doi:10.1145/2382577.2382579

Have a question like this one on your own data? Tell us what you need in a few lines.

Send us the brief

Related pages

  • The positive control: a planted effectBefore concluding that nothing is there, check that the test could have seen something. A positive control adds an effect of known size to the data and measures how often the analysis detects it.
  • Data leakage in machine learningA model leaks when something it was trained with would not be available at prediction time, or was computed with the test data. The score then measures the leak. The most common form hides in a preprocessing step.
  • The sealed test set, read onceA holdout sample gives an unbiased estimate once. Each time a decision is taken after looking at it, a little of it passes into the model. After enough rounds the reported score describes the holdout, not the future.
All library pages