Professor Turing

Study guides

What is cross-validation?

Last updated: 2026-11-23

Cross-validation estimates how well a model will do on data it has not seen: split your data into k parts called folds, train on all but one fold, score the model on the fold you left out, and repeat until each fold has been left out once. The average of those scores is your estimate. It matters because a model scored on the same data it learned from gives an overly hopeful picture of how it will do.

Start learning No account needed

1. Cross-validation in plain words

The scikit-learn user guide puts the problem this way: "Learning the parameters of a prediction function and testing it on the same data is a methodological mistake." A model can score well on the points it was fit to by matching their quirks, and then do worse on new points. So you hold some data back and score the model only on that.

One held-back set gives one score, and that score depends on which points happened to be held back. K-fold cross-validation gives you k scores from k different held-back sets, so you can see how much the score moves. With k = 5 and 10 data points, each fold holds 2 points, and the model is trained 5 times on 8 points each.

2. A worked example: 10 points, 5 folds, 2 models

The numbers below are made up for this page. Each point is a pair (x, y), and the goal is to predict y from x.

x12345678910
y35487912101314

We compare two models. The training-mean model predicts the average y of the training points for any x. The nearest-point model predicts the y of the training point whose x is closest. We score each prediction with the squared error (actual minus predicted, squared), and a fold's score is the mean of its two squared errors. After shuffling, the five folds are the points with x = {2, 3}, {6, 7}, {1, 10}, {8, 9} and {4, 5}.

Here is fold 1, where x = 2 and x = 3 are held out and the other 8 points are the training data.

  • Training-mean model: the training y values are 3, 8, 7, 9, 12, 10, 13, 14, which add up to 76, so the prediction is 76 / 8 = 9.5. The squared errors are (5 - 9.5)2 = 20.25 and (4 - 9.5)2 = 30.25, so the fold score is (20.25 + 30.25) / 2 = 25.25.
  • Nearest-point model: the closest training point to x = 2 is x = 1, so it predicts 3 and the squared error is (5 - 3)2 = 4. The closest to x = 3 is x = 4, so it predicts 8 and the squared error is (4 - 8)2 = 16. The fold score is (4 + 16) / 2 = 10.

Repeating that for all five folds gives these scores, where lower is better.

FoldHeld out (x)Training-meanNearest-point
12, 325.2510.00
26, 78.504.00
31, 1030.252.50
48, 916.312.50
54, 51.8110.00

The cross-validation estimate is the average of the five fold scores. For the training-mean model that is (25.25 + 8.50 + 30.25 + 16.3125 + 1.8125) / 5 = 82.125 / 5 = 16.43 after rounding (the table rounds folds 4 and 5 to two places). For the nearest-point model it is (10.00 + 4.00 + 2.50 + 2.50 + 10.00) / 5 = 29.00 / 5 = 5.80. The nearest-point model has the lower estimated error, which fits the data: y climbs with x, and the training-mean model ignores that.

The script's average of the five fold scores is 16.43 for the training-mean model and 5.80 for the nearest-point model, the same figures as above. These come from a short script, cross-validation-example.mjs, that does the same arithmetic.

3. A common misconception: one fold's score is the answer

It is tempting to read one fold, or to keep the model from the fold that scored lowest, as the result. Look at the nearest-point scores: 10.00, 4.00, 2.50, 2.50 and 10.00. If you had held out only fold 3, you would have reported 2.50, and with only fold 1 you would have reported 10.00, for the same model on the same data. The five scores are there to show how far a single split can move, and the average is the steadier number to report.

The folds are also not candidates for the final model. Cross-validation estimates how a modeling recipe does. Once you have picked the recipe, you fit it again on all 10 points and use that model. The scikit-learn guide adds that a separate test set should still be held out for the final evaluation, because choosing among models by their cross-validation scores lets knowledge of the held-out data "leak" into the choice.

With 10 points, even the average is rough. The point of the small example is to show the arithmetic, and a real project usually has far more data per fold.

4. Check that you understand it

Close this page and explain to someone else, or to an empty room, why a model's score on its own training data is not enough, and what happens to the data in 5-fold cross-validation. Then reopen the page and compare. The first place your explanation drifted is the part to study again.

For the general method, see how to test yourself.

What the sources found, and their limits

scikit-learn user guide, cross-validation. It describes splitting the training set into k folds, training on the rest and averaging the scores, and says a test set should still be held out for final evaluation. Limits: the page loaded only in part when we read it on 2026-11-23, so we used the passages quoted here and did not read the sections on the many splitting variants. The worked example, its data and its two models are ours, and they are made up to show the arithmetic.

How Professor Turing helps

Professor Turing is a tutor for adults learning any subject, in college or on their own. It asks you questions instead of giving the final answer, does not grade a bare guess, and remembers what you got wrong. No account is needed to start.

Bring a topic you are studying, such as how cross-validation scores a model. Its first reply asks what you already know before it teaches anything. If you answer with a bare guess, such as a term, it asks how you got there instead of saying right or wrong, and when you stay stuck it changes its approach and makes the next step smaller instead of giving the final answer. Later in a session it can offer a short test without help, scored apart from the tutoring, so you can tell what you can do alone from what the tutor carried you through.

It does not read or mark a written answer, and it does not write your explanation for you, so the explanation has to be yours. You must be 18 or older, and guest conversations are deleted 7 days after the last activity.

FAQ

Does a bigger k give a better estimate? Not necessarily. A larger k means each model trains on more of the data, and you train more models, which takes longer. In our example, k = 5 on 10 points leaves 2 points in each fold, which makes each fold score rough, and a larger k would leave even fewer.

Do I train a new model after cross-validation? Yes. Cross-validation scores a modeling recipe. Once you have chosen one, you fit it on all of your data, and that fitted model is the one you use.

Start with what you're working on

Tell the tutor which topic you are studying and let it ask you the questions.

Start learning No account needed

See also: all study guides