Contents

## What is Resampling?

Resampling is a technique of repeatedly drawing samples from available training data and refitting our model of interest to each of these samples to get additional information about our model.

For example, let’s say we want to build a regression model to evaluate a target variable. If we take different samples from the available data, each time we would get a different model as per the data used to train the model. We can then analyze the variability of the models and get additional information, which was not possible with only one sample of training data.

As Resampling involves redrawing and fitting the samples repeatedly, they are computationally very expensive. There are mainly two categories of resampling methods, as listed below.

- Cross Validation
- Bootstrapping

In this post, we will discuss briefly about Cross-Validation, it’s need, and the various types of Cross-Validation Techniques. The code used to describe the concepts are also included later in this post as jupyter notebook.

## What is Cross-Validation?

Cross-validation is a resampling technique for model evaluation where multiple models are fit on a subset of data, and the model is evaluated on the complementary subset of the data.

An optimal model should generalize well and should have a low test error. In real scenarios, the designated test data set is not available. In cross-validation, a dataset is divided into two subsets, a training dataset, and a validation/test dataset. An ML model is trained on the training dataset and evaluated against the validation dataset. Different models are built using different samples of training data and are evaluated against the complementary subset of the data. These different models give an idea about the performance of the model on unseen data. The bias-variance tradeoff, overfitting, etc. can be identified using cross-validation.

Cross- Validation are mainly used in the following scenarios.

**Model assessment**– To estimate the test error associated with a given ML method to determine it’s performance**Model selection**-To select an appropriate level of flexibility or suitable hyperparameters.

## Types of Cross-Validation

Cross-Validation is further categorized mainly into the following subtypes

- The Validation Set approach
- Leave One Out Cross Validation (LOOCV)
- K Fold Cross Validation

## The Validation Set Approach

The validation set approach is very simple strategy in which the available data is randomly divided into two parts, namely the training set and validation set or holdout set. The model is trained on the training set, and the fitted model is used to predict/classify the target value on the validation set. The model’s performance is then measured in terms of MSE/Misclassification Error depending upon whether the target variable is quantitative or qualitative, respectively. The validation set approach is shown in Figure 2 below.

In the Jupyter notebook included later in this post, we have applied The Validation set approach to the Auto-MPG dataset to estimate the test error that results from predicting mpg using polynomial functions of horsepower. In Figure 3, we have shown the test error estimates for the single split into training and validation data sets.

In Figure 4, we have repeated the validation set approach ten times, each time using a different random split of observations into a training set and validation set.

The validation set approach is easy to implement and conceptually simple but suffers from two major drawbacks.

- The validation estimate of the test error is highly variable in this method as can be seen in Figure 4. The test error depends upon the observations included in the training set and validation set, which is also variable.
- The validation estimate of the test error often overestimates the actual test error (had the model trained using entire data).

## Leave One Out Cross Validation

Leave One Out Cross Validation is closely related to the Validation set approach but tries to address the drawback of the validation set approach. In LOOCV, the available data is divided into two parts so that a single observation is kept for validation purposes. If there are n observations available, then n-1 observations are used for training purposes, and one observation is used for validation purposes.

This process is repeated such that all the observations are used for validation once. Hence, for n observation, the model needs to be trained n times. Let’s say the first time the model is trained on n-1 observations and tested on the remaining one observation. The test error so obtained is denoted as MSE1. Similarly, we get other MSEs are MSE2, MSE3, MSE4 and so on..

The LOOCV estimate for the test MSE is the average of these n test error estimates.

A schematic of the LOOCV is illustrated in Figure 5.

LOOCV has some advantages over the validation set approach.

- It has less bias.
- The LOOCV approach tends not to overestimate the test error rate as much as the validation set approach does.
- In contrast to the validation approach, which will yield different results when applied repeatedly due to randomness in the training/validation set splits, performing LOOCV multiple times will always yield the same results: there is no randomness in the training/validation set splits.

LOOCV is used on the Auto-MPG dataset to obtain an estimate of the test set MSE that results from fitting a linear regression model to predict mpg using polynomial functions of horsepower. The results are shown in Figure 6.

## K Fold Cross Validation

In k-fold cross-validation, the original sample is randomly partitioned into k different equal-sized subsamples. Of the k subsamples, a single subsample is retained as the validation data for testing the model, and the remaining k −1 subsamples are used as training data. The cross-validation process is then repeated k times, with each of the k subsamples used exactly once as the validation data. The k results can then be averaged to produce a single estimation.

The mean squared error, MSE, is computed on the observations in the held-out fold. This process results in k estimates of the test error, MSE1, MSE2,…, MSEk. The k-fold CV estimate is computed by averaging these values,

K-fold cross-validation is illustrated in Figure 7.

Figure 8 displays nine different 10-fold CV estimates for the Auto data set, each resulting from a different random split of the observations into ten folds. As we can see from the figure, there is some variability in the CV estimates as a result of the variability in how the observations are divided into ten folds. But this variability is typically much lower than the variability in the test error estimates that results from the validation set approach, as in Figure 4.

The K fold cross-validation has the same properties as that of LOOCV but is less computationally intensive. In K fold cross-validation, computation time is reduced as we repeated the process only ten times when the value of k is 10. The variance remains low, and as we increase the value of k variance is reduced. As the folds increase, most of the data is used by the model, and hence the bias is also reduced.

## Example Jupyter Notebook

## Summary

In this post we

- Introduced the concept of resampling and its types
- Discussed Cross-Validation and its need
- Discussed the various types of Cross-Validation
- Explained the code discussing cross-validation in the jupyter notebook

## No Responses Yet