الانتقال إلى المحتوى
Machine Learningتقييم الموديلات· 7 دقائق للقراءة

What Cross-Validation Is For: Estimating Performance, Not Picking a Fold's Model

The fold models are scaffolding and the best fold is the luckiest split. Report the mean and spread, then refit on all your training data.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
  • scikit-learn 1.9
محتويات المقال

A developer trains a tumor classifier on scikit-learn's breast cancer dataset and runs 5-fold cross-validation with return_estimator=True. One fold scores a perfect 1.000. They pickle that fold's model, and the model card reads "accuracy: 100% (cross-validated)". On the held-out test set the model scores 0.958, exactly what a plain model trained on all the data gets. The "winning" fold model was never special.

The common misconception

Many developers see cross-validation as a way to produce the final model: run k folds, keep the model from the best one, and report its score. It's easy to see why:

  • cross_validate(..., return_estimator=True) hands you k fitted models, and they look like candidates.
  • Keeping the highest scorer looks like model selection, which is what cross-validation is famous for.
  • The best fold's score is real: it was measured on rows that model never trained on.

The half-truth: every fold score is a genuine held-out score. But each one measures a single split of the data, and the k models are trained the same way on almost the same rows. Picking among them picks among splits, not models.

What actually happens

answers one question: if I train this recipe on data like mine, how well will the result do on rows it hasn't seen? It answers by building temporary models and throwing them away.

It evaluates a recipe, not a model

The scikit-learn user guide describes the loop: a model is trained on k-1 folds, validated on the remaining one, and the performance reported by k-fold cross-validation is the average of the values computed in the loop. What's being scored is the procedure, the preprocessing, the algorithm and its , applied to a training set. That's why cross_val_score returns an array of scores and nothing else. cross_validate can keep the fold estimators if you ask for them, which is useful for inspecting coefficients across folds, not for shipping.

Fold scores differ because the folds differ

With 426 development rows and 5 folds, each fold model trains on about 341 rows that overlap heavily with every other fold's training rows. The models end up nearly alike. What changes is the test fold: about 85 different rows each time, where a single mistake moves accuracy by more than a point. The fold that scored 1.000 got the easiest 85 rows, not the best model.

Python
from sklearn.datasets import load_breast_cancerfrom sklearn.linear_model import LogisticRegressionfrom sklearn.model_selection import cross_validate, train_test_splitfrom sklearn.pipeline import make_pipelinefrom sklearn.preprocessing import StandardScalerX, y = load_breast_cancer(return_X_y=True)X_dev, X_te, y_dev, y_te = train_test_split(X, y, test_size=0.25,                                            stratify=y, random_state=0)model = make_pipeline(StandardScaler(), LogisticRegression())cv = cross_validate(model, X_dev, y_dev, cv=5, return_estimator=True)best = cv["test_score"].argmax()deployed = cv["estimator"][best]  # "the winning fold's model"print(f"fold scores {cv['test_score'].round(3)}")print(f"reported    {cv['test_score'][best]:.3f}")print(f"test set    {deployed.score(X_te, y_te):.3f}")# fold scores [0.953 0.976 0.965 0.976 1.   ]# reported    1.000# test set    0.958

The same recipe scores between 0.953 and 1.000 depending only on which rows land in the test fold. Keeping the maximum of five noisy scores reports the luckiest one, 1.000, for a model that scores 0.958 on new rows. The mean of all five, 0.974 with a standard deviation of 0.015, is the honest estimate, and the test score sits about one standard deviation below it, well within the noise the folds already showed.

Refit on everything, then ship

A fold model has also seen only 80% of your development data. Deploying it throws away a fifth of your labeled rows for no benefit. Once cross-validation has told you the recipe works, train it one more time on all the development data: that's the model you ship. Here it scores 0.958 on the test set, the same as the "winning" fold model, with no lucky split behind its reputation.

scikit-learn's search tools follow the same pattern. GridSearchCV uses cross-validation to compare settings, then, with refit=True, its default, refits an estimator with the best parameters on the whole dataset you passed to fit. best_estimator_ is that refit model, not one of the fold models.

Report the spread, not just the mean

The standard deviation across folds tells you how much the estimate depends on the split. The scikit-learn guide's own example prints the mean accuracy together with its standard deviation. When two recipes differ by less than that spread, the gap may be noise; a later article in this path covers comparing models properly.

MythReality
Cross-validation trains the model you deployIt trains k throwaway models to estimate a recipe's performance
The best fold holds the best modelIt held the easiest rows: the 1.000 fold's model scored 0.958 on the test set
Report the best fold's scoreReport the mean and spread of all folds: 0.974 ± 0.015
Cross-validation replaces the final training runAfter cross-validation, refit the recipe on all the training data

How to apply it in your project

  1. Put the whole recipe in a pipelineWrap preprocessing and the model in a Pipeline, so each fold fits the scaler and the model only on its own training part.
  2. Report mean and spreadRun cross_val_score or cross_validate on the development data and report the mean with the standard deviation, not any single fold.
  3. Choose recipes, not fold modelsUse cross-validation results to compare settings, features or algorithms. Never pick among the fold models themselves.
  4. Refit the winner on all development dataCall fit on the full development set, or let GridSearchCV refit it, and ship that model.
  5. Confirm once on the test setScore the refit model on the held-out test set one time, and report that number alongside the CV estimate.

Key takeaways

  • Cross-validation estimates how a recipe performs on unseen data; it doesn't build your final model.
  • Fold scores differ by the luck of the split: the 1.000 fold's model scored 0.958 on the test set.
  • Report the mean and spread across all folds, 0.974 ± 0.015 here, never the best fold.
  • Refit the chosen recipe on all your training data, then confirm it on the test set once.

📚 المصادر

  1. Cross-validation: evaluating estimator performance — scikit-learn User Guide
  2. cross_val_score — scikit-learn API Reference
  3. GridSearchCV — scikit-learn API Reference

التعليقات (0)