Cross-validation and group folds

Folds, fold strategies, keeping groups together with a group column, and time-ordered folds.

Cross-validation gives a more honest score than a single split. The training rows are divided into parts called folds. The model is trained on all folds but one and scored on the one left out, once per fold, and the scores are averaged. The leaderboard values are these averages.

Hold-out and folds

mlflow uses two kinds of evaluation together:

  • Cross-validation on the training rows. It produces the leaderboard metrics.
  • A hold-out set: rows kept aside and never used for training or fold scoring. By default the engine keeps 30 percent of the rows (70 percent for training). Evaluation plots such as the confusion matrix or the residual plots use these rows.

For classification and regression, the Cross-validation card of the Models part sets CV folds and Fold strategy. Unsupervised tasks have no cross-validation. Time series folds are set in Forecast settings.

Fold strategies

StrategyTasksWhat it does
Stratified k-foldClassification (default)Random folds that keep each class's share the same in every fold.
K-foldClassification, regression (default for regression)Random folds.
Group k-foldClassification, regressionKeeps every group of rows in one fold. Needs a group column.
Stratified group k-foldClassificationKeeps groups together and balances classes across folds. Needs a group column.

Random folds are shuffled using the run's seed, so the same run gives the same folds.

Group column

Use a Group column when several rows belong to the same thing: a patient with many visits, a store with many days, a user with many sessions. If related rows are spread across folds, the model sees almost the same row in training and in the fold it is scored on, and the score is too optimistic. This is a kind of leakage.

With a group column, every group stays whole: in one fold, and on one side of the train and hold-out split. The share held out then counts groups, not rows. The group column:

  • is not used as a feature;
  • cannot be the target;
  • cannot be used by a pipeline step (the pipeline is rejected with an explanation if a step uses it);
  • must have at least as many groups as folds in the training rows.

Picking a group column moves a plain strategy to its group counterpart (Stratified k-fold becomes Stratified group k-fold for classification, otherwise K-fold becomes Group k-fold), and clearing it moves back. The card warns "This fold strategy needs a group column" if you choose a group strategy without one, and the reverse, and training is blocked until it is consistent.

Group folds do not apply to time series or to unsupervised tasks.

Time series folds

Folds must follow time order, so random and group strategies are not available. Each fold trains on the past and forecasts the next block of periods equal to the horizon.

  • Expanding: every fold trains on all history up to its cutoff.
  • Sliding: every fold trains on a window of the same length that moves forward.

The horizon can be 1 to 500 and the folds 2 to 10 (default 3). The series needs at least (folds + 1) × horizon + 1 rows. For example, a horizon of 6 with 4 folds needs 5 × 6 + 1 = 31 rows. See Time series forecasting.

Choosing the number of folds

CV folds can be 2 to 20 for classification and regression; the default is 10.

  • More folds give a steadier score because each model trains on more data, but take proportionally longer.
  • Fewer folds are faster. Five is a common choice for large datasets or slow algorithms.
  • Every class in a classification target needs rows in every fold, so a rare class limits how many folds you can use.
  • With a group column you need at least as many groups as folds.

Next steps

Learn the theory

Lessons on learning.mlflow.es that explain the ideas behind this page.

Updated 2026-11-22