Recommended resources
The freely available resources this lesson will direct you to.
- Lecture 8 - Data Splits, Models & Cross-Validation | Stanford CS229: Machine Learning (Autumn 2018)www.youtube.com
- Complete Guide to Cross Validationwww.youtube.com
- The impact of K selection in K‑fold cross-validation on bias and variance in supervised learning models | Scientific Reportswww.nature.com
- [PDF] STAT 451: Intro to Machine Learning Lecture Notessebastianraschka.com
Hello! Welcome to the next lesson in our module on model evaluation.
In our last session, we saw how learning curves, derived from the train/validation split, can be powerful diagnostic tools for identifying underfitting and overfitting. We established that evaluating a model on unseen data is fundamental for estimating its generalization capability. However, we ended on a crucial question: what if our dataset is too small to afford a large, held-out validation set? This approach not only wastes precious data that could be used for training but also leads to a performance estimate that can be noisy and highly dependent on the single, random split.
Today, we will address this challenge by exploring k-fold cross-validation. This powerful and widely used technique provides a much more robust and data-efficient method for estimating a model's generalization performance. We will delve into the mechanics of this procedure, analyze its properties through the lens of the bias-variance tradeoff, and understand how to integrate it into a rigorous model selection workflow.
The Instability of a Single Split
The core weakness of the simple train/validation split is that the resulting performance metric can be unreliable. If we happen to get an "easy" or "hard" validation set by chance, our estimate of the model's performance will be overly optimistic or pessimistic. Furthermore, by holding out a chunk of data, we are training our model on a smaller dataset than we have available, which can lead to a pessimistically biased estimate—the model may seem worse than it would be if trained on all available data.
The following video provides an excellent practical demonstration of why simply training a model and evaluating it on the same training data gives a dangerously over-optimistic result. It then shows how a single hold-out set gives a more realistic, but still potentially flawed, score. This sets the stage perfectly for needing a more robust method.
Complete Guide to Cross Validation
This video, "Complete Guide to Cross Validation" by Rob Mulla, begins with a compelling example of a model that seems nearly perfect on its training data but fails significantly on a held-out test set.
Please watch from the beginning to the conclusion of the holdout set evaluation. The key takeaway is to observe the dramatic difference between the performance on the training data and the performance on the unseen holdout data. This discrepancy is the fundamental problem that robust evaluation techniques must solve.
The K-Fold Cross-Validation Procedure
K-fold cross-validation (CV) solves the problems of data inefficiency and high-variance estimates by systematically using every part of the data for both training and validation. The procedure is as follows:
- Partition: The entire training dataset is randomly shuffled and partitioned into k equal-sized subsets, called "folds". Common choices for k are 5 or 10.
- Iterate: The process iterates k times. In each iteration i:
- Fold i is held out as the validation set.
- The remaining k-1 folds are combined and used as the training set.
- The model is trained on this training set and evaluated on the validation set (fold i). The performance score for that fold is recorded.
- Aggregate: After k iterations, we have k performance scores. The final cross-validation performance is the average of these k scores.
This process ensures that every single data point is used in a validation set exactly once, and is used for training k-1 times.

Let's ground this visual intuition with a more formal description. The following lecture notes from Sebastian Raschka provide a concise and clear explanation of the core idea.
[PDF] STAT 451: Intro to Machine Learning Lecture Notes
This document provides excellent theoretical notes on various ML topics. We'll focus on the introduction to k-fold cross-validation.
Please read Section 10.4, "Introduction to K-Fold Cross-Validation". Pay close attention to the distinction made between the holdout method and k-fold CV. The key benefits mentioned—using all data for training and testing and the non-overlapping nature of the validation folds—are central to its advantages.
By averaging the results over k folds, we obtain an estimate of the model's performance that is less sensitive to the specific composition of any single validation set. This results in a performance estimate with lower variance than what you would get from a single train/validation split.
Furthermore, in each iteration, the model is trained on a larger proportion of the data (e.g., for k=10, we use 90% of the data for training) compared to a typical holdout split (e.g., 70%). This leads to a less pessimistically biased performance estimate.
The Choice of K: A Bias-Variance Tradeoff for the Estimate
You might now be wondering: how do we choose k? This choice itself introduces a fascinating bias-variance tradeoff, not for the model, but for the performance estimate.
-
A large k (e.g., k = N, where N is the number of samples, known as Leave-One-Out Cross-Validation or LOOCV):
- Bias of the estimate: Very low. In each fold, the model is trained on N-1 samples, which is almost the entire dataset. The performance of this model is a very good, almost unbiased, proxy for the performance of a model trained on all N samples.
- Variance of the estimate: Very high. The k training sets are nearly identical to each other (overlapping by N-2 samples). This high correlation between the models trained in each fold leads to the average of their performances having a high variance.
- Computational Cost: Extremely high, as the model must be trained N times.
-
A small k (e.g., k = 2 or 3):
- Bias of the estimate: Higher. The model is trained on a smaller fraction of the data (e.g., 50% for k=2), resulting in a more pessimistically biased estimate of generalization performance.
- Variance of the estimate: Lower. The training sets are less overlapping, meaning the trained models are more independent, and their averaged performance is more stable.
- Computational Cost: Low.
The common practice of choosing k=5 or k=10 is an empirical sweet spot that has been found to provide a good balance in this tradeoff for a wide range of problems.
The lecture notes we looked at earlier contain an excellent, more advanced discussion on this topic. It's a prime example of the theoretical depth that underpins practical machine learning heuristics.
[PDF] STAT 451: Intro to Machine Learning Lecture Notes
Let's return to the same notes to explore the bias-variance tradeoff of the estimate itself.
Please read Section 10.6, "K-fold Cross-Validation and the Bias-Variance Trade-off". This section formally discusses why LOOCV can have high variance and summarizes the key research findings (like Kohavi's suggestion for k=10) that inform our practical choices. The summary points at the end are an excellent encapsulation of the key ideas.
This is not just a settled topic from old textbooks. A recent paper in Scientific Reports from 2024 revisited this exact question, analyzing how the choice of k affects bias and variance for modern algorithms. Their findings largely confirm the classic theory: variance consistently increases with k. This highlights that even foundational concepts are subject to ongoing research and validation.
This recent research paper provides an empirical look at the effect of k.
First, read the Abstract to get a high-level summary of the findings. Then, jump to the Discussion section to see how the authors interpret their results in the context of prior work and offer practical advice. This shows how the theoretical concepts we've discussed are actively tested and debated in the field.
Practical Variations and Final Workflow
While standard k-fold CV is powerful, certain situations call for specialized variants.
- Stratified K-Fold: When dealing with a classification problem, especially with imbalanced classes, we need to ensure that each fold is representative of the overall class distribution. Stratified K-Fold does exactly this, preserving the percentage of samples for each class in every fold. This is the default choice for most classification tasks.
- Group K-Fold: In some datasets, observations are not independent. For example, you might have multiple measurements from the same patient or sensor. To prevent data leakage (where information about the validation set inadvertently informs the training process), Group K-Fold ensures that all observations from the same group are always in the same fold (either all in training or all in validation).
The video from earlier provides clear, visual explanations of these important variations.
Complete Guide to Cross Validation
Let's return to the Rob Mulla video to see these variations explained.
Watch the following segments: K-Fold, which recaps the basic procedure. Stratified K-Fold, which is the most common and important variant for classification. Group K-Fold and Stratified Group K-Fold, to understand how to handle grouped data. Finally, watch the section "Back to the First Example". This is a critical segment that shows exactly how to implement Stratified Group K-Fold to get a robust performance estimate for the initial problem.
The Complete Workflow
It's crucial to understand how k-fold CV fits into the overall model selection process, which uses the train/validation/test split methodology we discussed previously.
- Split Data: Split your entire dataset once into a training set and a test set. The test set is locked away and not touched until the very end.
- Hyperparameter Tuning: Use k-fold cross-validation on the training set to find the best hyperparameters. For example:
- For a range of possible values for a hyperparameter (e.g., regularization strength
lambda), perform k-fold CV for each value. - Calculate the average performance for each
lambda. - Select the
lambdathat yielded the best average cross-validated performance.
- For a range of possible values for a hyperparameter (e.g., regularization strength
- Final Training: Retrain your model using the best hyperparameter value found in the previous step, but this time, train it on the entire training set. This uses all available training data to build the best possible final model.
- Final Evaluation: Evaluate the performance of this final model on the held-out test set. This single number is your unbiased estimate of the model's generalization performance on new, unseen data.
Raschka's notes provide a perfect diagram and step-by-step description of this exact workflow.
[PDF] STAT 451: Intro to Machine Learning Lecture Notes
Finally, let's look at the section on model selection, which puts everything together.
Read Section 10.7, "Model Selection via K-fold Cross-Validation". The diagram in Figure 7 and the accompanying step-by-step explanation precisely outline this critical workflow.
Conclusion
In this lesson, we have moved beyond a simple train/validation split to a more statistically robust and data-efficient evaluation technique. K-fold cross-validation is a cornerstone of applied machine learning, allowing us to make better decisions about model performance and hyperparameter choices, especially when data is limited.
Here are the key takeaways:
- K-fold cross-validation provides a more reliable estimate of model performance by averaging over multiple train/validation splits, reducing the variance of the estimate.
- By training on
k-1folds in each iteration, it uses data more efficiently and results in a less pessimistically biased estimate than a simple holdout method. - The choice of
kis a bias-variance tradeoff for the estimate itself, with k=5 or k=10 being common choices that balance computational cost with estimation accuracy. - Stratified K-Fold is the standard for classification tasks, as it preserves class proportions in each fold.
- K-fold CV is a tool for hyperparameter tuning and model selection performed on the training set, not a replacement for a final, held-out test set.
We now have a robust method for estimating how well our models perform. However, an estimate is only as good as the metric used. So far, we've vaguely referred to "performance," but what does that actually mean for a classification problem? Is accuracy always the right metric? What if false positives are more costly than false negatives?
In our next lesson, we will begin to answer these questions by diving into the confusion matrix and deriving the fundamental metrics of accuracy, precision, and recall from it.