LearnMachine learning

Cross-validation

Test a model fairly by letting every part of the data take a turn as the test set.

  • Machine learning
  • Fundamentals
  • Evaluation

Each block in the strip is one fold. Point at or tap a block to see its test points and its curve, or press "Run cross-validation". Drag any dot to change the data.

How it works

Every point gets a turn as the test.

A single split into training and test data can be lucky or unlucky. Cross-validation splits the data into k equal parts, called folds. Each fold takes one turn as the test set while the model trains on all the others.

The k test errors are averaged into one cross-validation score. Every point is used for testing exactly once, so the score is a steadier estimate of how the model will do on new data, and the gap between the best and worst fold shows how much it can vary.

Use it to compare models and settings, such as the degree of this curve, then train the final model on all the data. More folds give each model more training data but take longer to run; five or ten folds are common choices. If you also need an unbiased final score, keep a separate test set aside that cross-validation never sees.