A Complete Guide to Cross-Validation

A Complete Guide to Cross-Validation

Validation is key in building reliable machine learning (ML) models that learn to make predictions or perform other inference tasks upon data successfully. In essence, validation is a mechanism to assess how a newly built ML model performs on unseen data during its prior training stage. A validation process with positive outcomes ensures your model is generalized well beyond those previously seen data during training, that is, the training set.

While a simple train-test split of your data (see image below) can yield an initial glimpse of a model’s estimated accuracy, in real-world scenarios this is not enough. This simplified approach often falls short of producing a robust and reliable evaluation, particularly when the data available is limited. This is why more rigorous evaluation methodologies like cross-validation become indispensable.

A basic train-test data splitting for training an ML model
A basic train-test data splitting for training an ML model | Image by Author

What is Cross-Validation?

Whilst predominantly used in ML development workflows, cross-validation is a method with strong statistical roots. It is a statistical method used to assess the performance of advanced analytical models like ML ones systematically.

Cross-validation goes beyond a plain train-test split of the data where the training set is used alongside its known outputs or predictions (labels) to train the model, and then the test set examples are passed to the trained model (without their also known labels) to see how accurate the model predictions are. It partitions the data into several subsets or folds.

The most common approach to do this is k-fold cross-validation, where the data is split into k equal parts. The model is then trained on k-1 of these folds and tested on the remaining fold. The training process repeats k times so that each fold adopts the role of test set exactly once. The k results are finally averaged to provide a comprehensive performance metric: this approach provides a more reliable estimate of model performance and its ability to generalize well to future unseen data, thus preventing issues like model overfitting.

The original approach to k-fold cross-validation doesn’t leave aside a separate test set that is used for final validation on truly unseen data. To address this issue and probable consequences like data leakage, a more commonly used and robust solution is to apply k-fold cross-validation on the training set, after an initial train-test split. Multiple “versions” of the ML model are trained, each on k-1 folds of the training data.

The entire cross-validation process is typically applied for several configurations of the ML model hyperparameters: for instance, if you want to train a random forest model ensemble with three different settings of its n_estimators hyperparameter (the number of trees your ensemble is composed of), and you choose to apply 5-fold cross-validation, a total of 3 x 15 = 15 model versions will be trained across three validations. When averaging the results of each of the three validations associated with a model setting, you’ll choose as your most promising model the one whose hyperparameter setting yielded the highest aggregated accuracy after its associated cross-validation process with k-folds.

What will you do next with the chosen best candidate? Further validate it, of course, with the test set you left aside at the beginning!

K-fold cross validation on training data
K-fold cross validation on training data

Why it Matters & Key Best Practices

Cross-validation is important in training robust ML models because it helps find a trained model that minimizes both bias and variance issues that often arise in a simple train-test split, leading to problems like overfitting or underfitting. It also helps ensure that every data point is used for both training and validation, providing a comprehensive evaluation of the model’s performance.

Three key best practices for applying cross-validation are:

  • Choose the right k wisely: a larger k like 10 or higher is usually better when your training data are not large enough, although it will incur higher computational costs. In larger datasets the variance in model performance will naturally be lower because each fold is assumed to contain sufficiently representative data, hence a lower k will do the job.
  • Stratified folds: applying stratified sampling to split the data into folds ensures class and attribute-level balance per fold, which is crucial when working with imbalanced datasets.
  • Mind your data transformations: to avoid issues like data leakage, preprocessing steps such as encoding and scaling should be applied separately within each fold.

Wrapping Up

Now that you understand cross-validation and how to apply it, let’s wrap up with an example scenario: suppose you’re building a regression model to predict prices of new house listing based on the house features. Using 5-fold cross-validation, you may want to divide your training data — e.g. approximately 10000 past house listings with known prices — into five parts containing about 2000 examples each. Your model is trained on four parts of the training data (80% of it) and tests on the fifth (the remaining 20%), cycling through all folds. Your final performance metric chosen for that model, such as RMSE, is the average of RSMEs reported across all five folds.

In summary, cross-validation is a widely adopted evaluation approach to gain confidence not only in your ML model’s accuracy but most importantly in its ability to generalize to future unseen data, ensuring robust results for real-world scenarios.

Leave a Reply

Your email address will not be published. Required fields are marked *