Fitting boundaries, tensor contracts and masks
Every step that learns from data has a boundary. Fit imputation, scaling, encoders, calibrators and vocabularies on training rows only, respect time and graph access, and write down what each axis and mask means.
You will be able to
- Say which preprocessing steps learn from data and where they may be fitted
- Keep calibration and target encoding on rows the model didn't train on
- Respect time order, delayed labels and graph access when building features
- Tell padding, causal, observation and label masks apart
Which steps learn?
ReadA step learns from data when its output depends on the rows it saw: imputation values, scaling means and SDs, feature selection, vocabularies, PCA directions, learned embeddings, oversampling rules and target encodings. Each has a fitting boundaryFitting boundaryThe rows a learned step may learn from. Imputation values, scales, vocabularies and encoders fit on training rows, then apply unchanged.: fit it on the training part, then apply it unchanged to validation and test.
A deterministic step with fixed outside parameters is different. Converting pounds to kilograms or applying a published tokenizer learns nothing from your rows. Still record its version and what it assumes.
Refit the chosen pipeline on the whole development set only after the settings have been chosen.
Watch the leak happen
ExploreTwo hundred random features, sixty rows and an outcome that is pure noise. Pick the "best" features on all rows and then cross-validate, and the score looks impressive. Move the selection inside each fold and the score drops back to chance.
DExplorable · Leakage detective
Why does a model that scores brilliantly in testing fail on real data?
Read the pipeline on the left. Will its cross-validation score overstate real-world accuracy by more than 6 points?
The pipeline
Commit your prediction above to see the result
Make your prediction to see the result.
The model didn't change. Only where the selection step was allowed to look changed.
Encoders and calibrators need their own rows
ReadTarget encoding replaces a category with an average of the outcome. Computed on the same rows it encodes, each row partly sees its own answer. Cross-fittingCross-fittingEach row's learned value comes only from the other folds, so no row sees its own outcome. fixes that: each row's value comes only from the other folds.
Calibration is a separate stage. Fit the calibrator on held-out or cross-fitted predictions from a model that didn't train on those labels, never on the model's own training predictions. Keep that separation when you refit or ensemble.
Final-test labels stay out of both.
Time and graphs have boundaries too
ReadIn forecasting, the boundary also runs through time. A rolling mean may use only the past. Interpolation can't borrow future points the deployed system won't have. A label that arrives weeks later can't be a feature today. A "lifetime value" column computed over a customer's whole history can leak the outcome while looking innocent.
Graphs need a declared protocol. Using the full unlabeled network is fair in a transductive task, where every node is known at training time. It is a leak in an inductive one, where new nodes arrive later.
Tensor contracts and masks
ReadWrite a contract for every model input: axes, dtype, units, normalization, valid ranges, allowed missing values, padding and what each mask means. Four masks are easy to confuse:
| Mask | Says |
|---|---|
| Padding | Which positions exist |
| Causal | Which existing positions a step may see |
| Observation | Which measurements were actually taken |
| Label | Which positions count in the loss |
Mixing them up can train on padding or leak the future. Test each with a tiny hand-made input, including an empty one. Also state how losses average (per token, frame, example or entity) and which units every column uses.
Learned steps in Prepare and Predict
In the appA Prepare recipe's learned steps fit on Every row (the default) unless you change Rows learned steps fit on to Training rows only or a Split column (rows marked "test" are left out of fitting). Existing recipes keep fitting on every row, so their results don't move.
When an experiment's dataset came from a recipe whose learned steps saw every row, Predict's Setup step names those steps and offers Refit the recipe per fold: the recipe is recomputed for every fold, fitted on that fold's training rows only. It is off by default. Target encoding in Prepare can be cross-fitted.
Check: fitting boundaries
CheckpointCheck yourself
Which step does NOT need to be fitted on training rows only?
Show the answer
Answer: d
A unit conversion uses fixed outside constants and learns nothing from your rows. The other three take values from the data and must stay inside the boundary.
Check yourself
Features were picked on all 60 rows, then the model was cross-validated. The score is well above chance on a noise outcome. Why?
Show the answer
Answer: a
Selection is part of fitting. Done on all rows, it uses the validation labels, and the score measures that leak, not the model.
Check yourself
Where should a calibrator for a boosted model be fitted?
Show the answer
Answer: b
Training predictions are overconfident, and the test set must stay unseen. Out-of-fold predictions give the calibrator honest inputs.
Check yourself
A feature is the customer's total spend over their whole history, including months after the prediction date. What is wrong?
Show the answer
Answer: b
The information time in the task contract decides which features are allowed. A feature computed with future data leaks the outcome.
Check yourself
Which mask says that position 7 may not look at position 9, even though both exist?
Show the answer
Answer: b
A causal mask hides later positions. A padding mask marks which positions exist at all; observation and label masks mark measured values and positions that count in the loss.
Check yourself
A fraud model on a transaction network will score accounts opened after training. During development it used the whole network, including those later accounts' links. Is that fair?
Show the answer
Answer: b
Using the full graph is fine when every node is known in advance (transductive). Here new nodes arrive later, so using their links leaks future structure.
Check yourself
Predict warns that the dataset's recipe learned its scaling from every row. What does "Refit the recipe per fold" do?
Show the answer
Answer: b
Each fold gets its own copy of the recipe, so validation and test rows never shape the learned steps. It is off by default, and existing runs keep their numbers.
Recap
Recap- Learned steps (imputation, scaling, selection, encoders, embeddings) fit on training rows and apply unchanged elsewhere.
- Target encodings are cross-fitted; calibrators fit on out-of-fold or held-out predictions.
- Features respect information time; graph access matches a declared transductive or inductive task.
- Contracts name axes, units and masks; test masks on tiny inputs.
- Prepare's fit scope and Predict's "Refit the recipe per fold" put the boundary into practice.
Concepts in this lesson