Skip to content
IT glossary
Data and MLUpdated December 3, 2026

Cross-validation

Кросс-валидация

This is a way to estimate how well a machine learning model will work on new data. You train and evaluate it on several splits of the available data, then summarize the scores.

Why use it?

A single validation split can make a model look better or worse than it really is. For instance, a classifier might score well because the validation set happens to contain mostly easy cases. Cross-validation checks performance across several splits, making the estimate less dependent on which examples land in one validation set. It cannot, however, tell you how the model will handle conditions that your data never captured.

How k-fold cross-validation works

Divide the data into k non-overlapping subsets called folds. Fit a fresh model on k − 1 folds and score it on the fold you left out. Repeat until each fold has served as the validation set exactly once. Report the mean score and, if useful, the individual scores or their spread. Fold scores are not independent because the training sets overlap, so their standard deviation is not a confidence interval.

Example: comparing two classifiers

Suppose you have 1,000 labeled support tickets and want a model to predict each new ticket's category. With five folds of 200 tickets, each fit uses 800 tickets and each evaluation uses 200. If the macro-F1 scores are 0.76, 0.80, 0.77, 0.79, and 0.78, the mean is 0.78. Use the same folds and metric for the second classifier so that you're not comparing one model's score on easy tickets with another model's score on harder ones. Five-fold validation requires five fits per candidate, so running it for many configurations can get expensive.

Match the split to deployment

  • Stratified folds keep class proportions approximately constant. This is useful when a category is uncommon and a random split might leave a fold with very few examples of it.
  • Grouped folds keep related records together. If you need to predict for customers the model has never seen, keep each customer's records on the same side of each split.
  • Time-based splits train on earlier observations and validate on later ones. For forecasting, shuffling can let the model train on observations from after the point at which it would have made its prediction.

Leakage and model selection

Fit preprocessing inside each fold. For example, calculate the values used to fill missing entries from that fold's training rows, then apply those values to its validation rows. If you calculate them before splitting, the validation data influences what the model learns. A scikit-learn Pipeline lets you evaluate preprocessing and the estimator together so that both are fitted separately for each split.

Use cross-validation to compare models or select hyperparameters, but don't treat the winning score as an independent estimate. After choosing a configuration, refit it on the full development set and evaluate it on an untouched test set. If you have no separate test set, nested cross-validation is an alternative: the inner loop tunes the model, and the outer loop evaluates the tuning procedure on rows it did not see.

What interviewers look for

Expect a scenario rather than just a request to define k. If one customer appears in several rows, explain how a row-level random split could let the model benefit from seeing that customer during training, and why that matters if deployment is aimed at new customers.