Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 37

K-Fold Cross-Validation for Model Evaluation and Regularization Tuning

Recommended resources

Hello! Welcome to the final lesson of our module on Linear Regression and Regularization.

In our previous lessons, we explored Ridge (L2) and Lasso (L1) regression, and you now have a solid geometric and practical intuition for why Lasso performs feature selection while Ridge does not. We saw that both of these powerful methods rely on a regularization parameter, (or ), which controls the strength of the penalty. This raises a crucial question: how do we choose the best value for ?

Today, we will answer that question. Our goal is to learn how to use k-fold cross-validation to evaluate model performance and select regularization parameters. This is one of the most fundamental and essential skills in applied machine learning, ensuring that you can build models that are not only powerful but also robust and well-tuned.

The Challenge of Model Evaluation

So far, we've split our data into a training set and a test set. We train the model on the training data and evaluate it on the test data. But how reliable is that single test score? What if, by pure chance, we got an "easy" test set, making our model look better than it is? Or an unusually "hard" one?

The score we get from a single train/test split can be misleading. This is a common pitfall for beginners who might report an overly optimistic result. The following video demonstrates this problem vividly by training a model that gets 99% accuracy on the training data but performs much worse on unseen data.

Complete Guide to Cross Validation

To see why we need a more robust evaluation method, watch this segment from the 'Complete Guide to Cross Validation' video by Rob Mulla. It provides a practical example of a model that seems amazing on training data but fails on a holdout set, highlighting the problem of overfitting and the limitations of a single evaluation.

Please watch from the beginning to 12:36. Focus on the stark difference between the model's score on the training data versus its score on the holdout (test) data. This demonstrates why evaluating on the data you trained on is a mistake and why even a single holdout set might not tell the whole story.

As you saw, relying on a single split gives us a shaky estimate of our model's true performance. We need a more reliable method to estimate how our model will perform on new, unseen data.


K-Fold Cross-Validation: A More Robust Approach

Instead of a single split, k-fold cross-validation allows us to use our data more effectively to get a more stable and reliable performance estimate.

Here’s the process:

  1. Split: We divide our training dataset into k equal-sized parts, or "folds" (e.g., 5 or 10).
  2. Iterate: We run k rounds of training and validation. In each round i:
    • The model is trained on k-1 folds.
    • The trained model is then validated on the remaining fold (the "hold-out" fold).
  3. Average: After k rounds, every fold has been used as the hold-out set exactly once. We then average the k performance scores (e.g., Mean Squared Error) to get our final cross-validation score.

This process is visualized beautifully in the following diagram and video.

K-Fold Cross-Validation Process
This diagram illustrates the k-fold process. The full dataset is repeatedly split. In each of the 'K' iterations, a different fold is used for validation (the test fold in blue), and the remaining folds are used for training (in green). The performance scores from all K iterations are then averaged.

To see this in motion, let's watch a quick and clear explanation.

Machine Learning Fundamentals: Cross Validation

The video 'Machine Learning Fundamentals: Cross Validation' from StatQuest provides a concise and clear explanation of the k-fold cross-validation mechanism.

Please watch from 03:02 to 05:20. This segment walks through the core idea of using each block of data for testing while training on the others, and then averaging the results.

By averaging the results over k different splits, we smooth out the randomness associated with a single split. The resulting score is a much more robust estimate of how the model is likely to perform on data it has never seen before.

Important Variations

  • Stratified K-Fold: When dealing with classification problems with imbalanced classes (e.g., 90% "no" and 10% "yes"), a random split might create a fold with no "yes" samples at all. Stratified k-fold ensures that each fold has the same percentage of samples for each class as the original dataset. We will revisit this when we cover classification.
  • Leave-One-Out Cross-Validation (LOOCV): This is an extreme case where the number of folds k is equal to the number of data points N. It's very thorough but computationally very expensive and is used less often in practice.

Using Cross-Validation to Select Regularization Parameters

Now we can connect this back to our main goal: choosing the best for our Ridge or Lasso model. This process is a type of hyperparameter tuning. The general strategy is called Grid Search.

The procedure is as follows:

  1. Define a Grid: First, we define a set of candidate values for , for example: [0.001, 0.01, 0.1, 1, 10, 100]. This is our "grid" of hyperparameters.
  2. Evaluate Each Candidate: For each value of in our grid, we perform k-fold cross-validation and calculate the average validation error (e.g., average MSE).
  3. Select the Best: We choose the that resulted in the lowest average validation error. This is our optimal hyperparameter.

This process effectively searches for the "sweet spot" in the bias-variance tradeoff.

Bias-Variance Tradeoff in Lasso Regression
This graph shows how Test MSE (black line) changes with the regularization parameter lambda (equivalent to \(\alpha\)). Cross-validation is the practical technique used to estimate this curve and find the optimal lambda that minimizes the MSE, balancing Bias (blue) and Variance (red).

The following reading explains how this hyperparameter search is done in practice using Scikit-Learn's GridSearchCV, which automates this entire process for you.

Hyperparameters and Model Validation

The 'Python Data Science Handbook' provides an excellent overview of using cross-validation for hyperparameter tuning. This section introduces the concept of grid search, which is the standard way to select parameters like alpha.

Please read the section titled 'Validation in Practice: Grid Search'. It explains how to define a parameter grid and use Scikit-Learn's GridSearchCV to automatically find the best model parameters. The example uses polynomial regression, but the principle is identical for tuning the alpha in Ridge or Lasso.

The Final Workflow

It's critical to follow the correct sequence of steps to avoid "data leakage," where information from your test set accidentally influences your model training or selection.

The complete, correct workflow is:

  1. Split your data once into a training set and a final test set. Put the test set aside and do not touch it until the very end.
  2. Perform k-fold cross-validation only on the training set to find the best hyperparameter ().
  3. Once you've identified the best , re-train your model on the entire training set using this optimal .
  4. Finally, evaluate this single, re-trained model on the held-out test set. This score is your final, unbiased estimate of the model's performance on new data.
Test your understanding!

You have used 10-fold cross-validation to find that the best alpha for your Lasso model is 0.1. You are now ready to deploy this model to make predictions on new, unseen data. What is the very next step you should take with your training data and the chosen alpha?

Show answer

The very next step is to re-train the Lasso model using alpha=0.1 on the entire training dataset. The models trained during the k-fold CV process were only for evaluation; they are discarded. You need one final model, trained on all available training data with the best hyperparameter, to use for future predictions.


Conclusion

You have now learned one of the most vital techniques in the machine learning practitioner's toolkit. K-fold cross-validation is the gold standard for both robustly estimating a model's performance and for tuning its hyperparameters.

Key Takeaways:

  • A single train/test split can produce a misleading evaluation of model performance due to the luck of the draw in the split.
  • K-fold cross-validation provides a more reliable performance estimate by training and validating the model on k different subsets of the data and averaging the results.
  • The process of using cross-validation to find the best hyperparameter is called grid search.
  • The final, correct workflow involves:
    1. Splitting into training and test sets.
    2. Using CV on the training set to find the best hyperparameters.
    3. Re-training a final model on the entire training set with those hyperparameters.
    4. Reporting the final performance on the held-out test set.

Preview of the Next Lesson:
We have now completed our deep dive into linear regression. We've covered everything from the basic model to regularization and hyperparameter tuning. In our next module, we will shift our focus from predicting continuous values (regression) to predicting discrete categories (classification). We will begin by learning how to model binary classification probabilities using the logistic (sigmoid) function, the foundation of logistic regression.