Tutorials › Regression Analysis (Undergraduate) › Choosing the Number of Folds

Train-Test Splits and Cross-Validation · Tutorial 749 of 991

Choosing the Number of Folds

See how changing the number of folds trades off training data, computational cost, and the stability of cross-validation results.

Intermediate 10 min read

What You'll Learn

  • Calculate the approximate training and validation sample sizes for a chosen number of folds.
  • Explain why increasing the number of folds usually increases model-fitting cost.
  • Distinguish variation among fold scores from variation in the overall cross-validation estimate.
  • Compare the practical tradeoffs of five-fold and ten-fold cross-validation.
  • Choose a fold count that fits the sample size, computing budget, and goal of the evaluation.

Why the Number of Folds Matters

In \(K\)-fold cross-validation, the data are divided into \(K\) parts, or folds. Each fold takes a turn serving as the validation set while the model is fitted using the other folds. The resulting held-out predictions are used to summarize how well the fitting procedure predicts observations it did not use for that fit.

Choosing \(K\) changes two things directly: how much data is available to fit each model and how many model fits are needed. It also changes the sizes and overlap of the validation and training sets, which can affect how variable the cross-validation results are. There is no single value of \(K\) that is best for every data set and prediction task. The useful choice balances these considerations.

Definition: In \(K\)-fold cross-validation, the \(n\) observations are assigned to \(K\) nonoverlapping folds. For each fold \(k\), the fitting procedure is applied to the observations outside that fold, and predictions are evaluated on the observations in the held-out fold. Each observation is held out once. The number of folds, \(K\), is an integer greater than 1 and no greater than \(n\).

For concreteness, suppose the prediction outcome is quantitative and performance is measured by mean squared error (MSE). Write \(I_k\) for the set of observation indices in fold \(k\), and \(\hat{f}^{(-k)}(x_i)\) for the prediction at predictor value \(x_i\) from a model fitted without fold \(k\). The cross-validation MSE is the average squared prediction error across all held-out observations:

$$ \mathrm{CV\ MSE}_K = \frac{1}{n} \sum_{k=1}^{K} \sum_{i\in I_k} \left(y_i-\hat{f}^{(-k)}(x_i)\right)^2 $$

This expression weights each observation equally. When fold sizes are equal, it is also the average of the \(K\) fold-specific MSEs. When fold sizes differ, averaging the fold-specific MSEs equally would give a small fold the same weight as a large fold, so the observation-level formula is the appropriate overall average.

The interpretation depends on the data and procedure. The folds must assign each observation to one held-out fold, and a held-out observation must not be used to fit the model that predicts it. The fitting and scoring procedures should be the same across folds. The data should also be suitable for the intended prediction target: for example, a randomly sampled set of individuals is not automatically an appropriate stand-in for predictions about a different population or a different time period. The fold assignment must respect important structure in the data when such structure is part of the prediction problem.

How \(K\) Sets Training and Validation Sizes

If the folds are as equal in size as possible, each contains approximately \(n/K\) observations. A model fit in one round therefore uses approximately

$$ n-\frac{n}{K} = n\left(1-\frac{1}{K}\right) $$

observations for training, while approximately \(n/K\) observations are held out for validation. These are approximate sizes when \(n\) is not divisible by \(K\): some folds must then contain one more observation than others.

Increasing \(K\) makes each training set larger and each validation fold smaller. A larger training set can make each fitted model more like a model trained on the full data set. But each individual validation score is based on fewer observations, so it can be more sensitive to which cases happen to be in that fold. These are tendencies, not guarantees about the final cross-validation estimate.

Worked Example: Comparing Training-Set Sizes

A hypothetical data set contains \(n=103\) observations. Compare the training and validation sizes for \(K=5\) and \(K=10\), assuming the folds are as equal as possible.

With five folds. Since \(103=3(21)+2(20)\), three folds contain 21 observations and two contain 20. When a 21-observation fold is held out, the training set has \(103-21=82\) observations. When a 20-observation fold is held out, it has \(103-20=83\) observations. The average training-set size is

$$ \frac{3(82)+2(83)}{5} = \frac{412}{5} = 82.4 $$

The formula gives the same result: \(103(1-1/5)=103(0.8)=82.4\). The average validation-fold size is \(103/5=20.6\).

With ten folds. Since \(103=3(11)+7(10)\), three folds contain 11 observations and seven contain 10. The corresponding training sets contain 92 or 93 observations. Their average size is

$$ \frac{3(92)+7(93)}{10} = \frac{927}{10} = 92.7 $$

Equivalently, \(103(1-1/10)=103(0.9)=92.7\). The average validation-fold size is \(103/10=10.3\). Ten-fold cross-validation therefore uses about 10.3 more training observations per fit on average than five-fold cross-validation, while its validation folds are about half as large.

Computational Cost and Common Choices

For one specified fitting procedure, \(K\)-fold cross-validation requires \(K\) fits: one for each held-out fold. If fitting a model takes roughly the same time for each fold, doubling \(K\) roughly doubles the time spent fitting that model. Prediction and scoring also require work, but the model fits are often the main cost. Actual time can differ if fit sizes differ substantially or the fitting algorithm behaves differently at those sizes.

Five-fold and ten-fold cross-validation are common practical choices. Five folds require fewer fits and use validation sets that are relatively large. Ten folds require twice as many fits as five folds, but use larger training sets. Neither choice automatically gives a more accurate estimate in every setting. The decision should reflect the available computing time, the sample size, and how important it is for each fitted model to use a large fraction of the data.

Worked Example: Counting Model Fits

A hypothetical analyst plans to evaluate four regression procedures on the same data, using cross-validation separately for each procedure. Suppose one fit takes about 12 seconds, regardless of fold size. Compare the fitting time for five-fold and ten-fold cross-validation, ignoring prediction and scoring time.

Five folds. Each procedure is fitted five times, so four procedures require \(4\times5=20\) fits. The estimated fitting time is

$$ 20\text{ fits}\times 12\text{ seconds per fit} = 240\text{ seconds} $$

This is 4 minutes.

Ten folds. Four procedures require \(4\times10=40\) fits. The estimated fitting time is

$$ 40\text{ fits}\times 12\text{ seconds per fit} = 480\text{ seconds} $$

This is 8 minutes, twice the fitting time in this simplified comparison. The calculation shows the direct cost of increasing \(K\); it does not show that the ten-fold estimate is twice as useful or twice as accurate.

What Happens to Variability?

It helps to distinguish two kinds of variation. Variation among fold scores means that the errors calculated in different held-out folds are not all the same. Variation in the overall cross-validation estimate means that the final average would change if the data or the fold assignment changed. Fold scores are pieces of one cross-validation calculation; they are not independent estimates whose spread directly determines the uncertainty of the overall result.

When \(K\) is larger, each validation fold is smaller. A fold-specific score based on fewer observations can respond strongly to a few unusually easy or difficult cases. At the same time, the models use more training data, which may reduce the effect of the smaller training sample compared with a lower \(K\). The training sets also overlap substantially, so the fold results are related to one another. Consequently, there is no general rule that increasing \(K\) must increase or must decrease the variability of the overall estimate.

The sample size matters to this tradeoff. With a small data set, even a moderate \(K\) may leave each validation fold with very few observations, making individual fold scores sensitive to case composition. A smaller \(K\) gives larger validation folds but trains each model on a smaller part of the data. A larger \(K\) trains each model on more observations but demands more fitting and creates smaller validation folds. These considerations motivate inspecting fold sizes before choosing \(K\), rather than treating a conventional value as universally appropriate.

Worked Example: Reading Fold Scores Carefully

Suppose a hypothetical data set has 30 observations and squared prediction error is measured in squared outcome units. One five-fold calculation gives fold sizes of 6 and fold MSEs of 18, 22, 20, 24, and 16. A ten-fold calculation gives fold sizes of 3 and fold MSEs of 10, 30, 14, 26, 12, 28, 16, 24, 18, and 22. Calculate both overall cross-validation MSEs and compare the fold-score spreads.

Five folds. The folds are equally sized, so the overall MSE is the average of the five fold MSEs:

$$ \mathrm{CV\ MSE}_5 = \frac{18+22+20+24+16}{5} = \frac{100}{5} = 20 $$

Ten folds. These folds are also equally sized, so

$$ \mathrm{CV\ MSE}_{10} = \frac{10+30+14+26+12+28+16+24+18+22}{10} = \frac{200}{10} = 20 $$

Both calculations produce an overall MSE of 20 squared outcome units. The five-fold scores range from 16 to 24, a range of \(24-16=8\). The ten-fold scores range from 10 to 30, a range of \(30-10=20\). In this particular hypothetical comparison, the ten-fold scores vary more across folds, but both procedures give the same overall estimate.

The larger range does not establish that ten-fold cross-validation generally has greater variability: it is one result on one data set with particular fold assignments. It does illustrate why smaller fold-specific scores can look more uneven. The spread across folds should not be mistaken for a direct measure of how much the overall estimate would change under other data or fold assignments.

A Practical Choice

A useful choice begins with the sizes and structure of the data, then accounts for the cost of fitting. Five folds are a reasonable practical option when fitting time matters or validation folds need to be reasonably large. Ten folds may be attractive when a larger training set in each round is important and the additional fitting is affordable. If either choice produces very small validation folds, or if the data have important grouping or time structure, the fold design needs particular care.

The choice of \(K\) does not repair an unsuitable sample, an inappropriate prediction target, or a poorly chosen performance measure. It controls how the available observations are divided between training and validation in each round. Interpret the resulting cross-validation score as an estimate for the fitting procedure and evaluation setup actually used.

Key takeaway: Increasing \(K\) generally increases training-set size and the number of model fits, while decreasing validation-fold size. Five and ten folds are practical conventions, not universal guarantees about accuracy or stability. Consider sample size, fold composition, computational cost, and the prediction target together.

Common Mistake

  • Wrong reasoning: “Ten-fold cross-validation uses more training data, so its estimate must be more accurate and less variable than a five-fold estimate.”
  • Correction: Ten folds do use larger training sets, but they also have smaller validation folds and require more fits. Smaller folds can produce more variable fold-specific scores, and the net variability of the overall estimate is not guaranteed to move in one direction. Choose \(K\) based on the full tradeoff, not on training-set size alone.

Check Your Understanding

Answer each question using the ideas and calculations in this tutorial.

  1. For \(n=80\), what are the approximate training and validation sizes for \(K=5\) and \(K=10\)?
  2. Why does ten-fold cross-validation usually require about twice as many fits as five-fold cross-validation for one model?
  3. What is the difference between variation among fold scores and variation in the overall cross-validation estimate?
  4. Why does a larger fold-score range in one ten-fold calculation not prove that ten-fold cross-validation is generally more variable?
  5. Name two practical considerations, other than convention, that can guide the choice of \(K\).