Module 2 · step 4 of 4 · Data and profiling
Split and validate
Why you keep data aside, how cross-validation gives a steadier score, and where to set it in mlflow.
Concept8 min
In this step you will learn why part of the data is kept aside, how cross-validation gives a steadier score, and where to read and set it in the Models part of Pipeline Studio. It matters because a score measured on rows the model already saw is flattering, and you would deploy on a false promise.
The idea
A model scored on the rows it learned from looks better than it is, because it can memorise them. To estimate how it will behave on new cases, you keep rows aside and never let the model learn from them. The rows used for learning are the training data; the rows kept aside are the hold-out (or test) data. The score on hold-out rows is the honest estimate.
One split has a weakness: the score depends on which rows landed in the hold-out set. Cross-validation reduces that luck. The training rows are divided into parts called folds. The model trains on all folds but one and is scored on the one left out, once per fold, and the scores are averaged. More folds give a steadier estimate but take longer.
Everything that learns from data must learn from training rows only: a scaler's mean, an imputer's fill value, an encoder's categories. If they see the hold-out rows, information leaks and the score is too optimistic. Two cases need care. If several rows belong to the same person, store or customer, keep each group on one side. If the data is a time series, folds must follow time, so you never train on the future.
Step by step
- Open the demo's churn pipeline from Pipelines in the project sidebar and select the Models part.
- Find the Cross-validation card. Read CV folds (from 2 to 20, default 10) and Fold strategy.
- Open the Fold strategy options: Stratified k-fold keeps the share of each class equal in every fold and is the classification default; K-fold splits at random; Group k-fold and Stratified group k-fold keep related rows together. Press "?" next to the field to read the explanation.
- When rows belong together (visits of one patient), choose a Group column, so every group stays whole in one fold.
- Change nothing yet. Open Review & run and select Check without running to see that the pipeline is valid.
- After a run, remember that the leaderboard metrics come from cross-validation on the training rows. A hold-out set (by default 30 percent of the rows) is never used for training or fold scoring, and evaluation plots such as the confusion matrix use it.
Preprocessing steps from the Columns part are fitted on training rows only, and the same fitted steps are applied to later rows. You get the correct order without doing anything, which is also why a deployed model accepts raw values.
Common mistakes
- Preparing the whole dataset (scaling, imputing, encoding) before splitting. In mlflow let the steps in Columns do it, so they are fitted on training rows only.
- Reporting the training score as if it were performance on new data.
- Splitting related rows, such as visits of one patient, across training and test. Use a Group column.
- Using random folds on a time series.
- Asking for more folds than a rare class can support. The run adapts and tells you: "Cross-validation used N folds instead of M: the smallest class has only K rows."
- Tuning against the hold-out score again and again until it no longer measures anything new.
As a data scientist
Decide the validation scheme before you look at results and match it to how the model will be used: new customers need group folds, tomorrow's forecasts need time order. Quote the cross-validated score, not one lucky split. In mlflow each run keeps its cross-validation settings, so you can always say how a score was produced. For depth, read Train and test split, Cross-validation and Data leakage.