### Introduction

The most beautiful aspect of machine learning is its ability to make predictions on the never seen before data points. In order to estimate the performance of ML models, we need to take our dataset and divide it into two parts. One part powers the model training whereas the other is used to make predictions. The former one is known as training dataset and the latter one is known as the test dataset

In this blog, we will study in depth the concept of cross-validation. We will further study about different cross-validation techniques too!

### What is Cross-Validation?

Cross-validation is an extension of the training, validation, and holdout (TVH) process that minimizes the sampling bias of machine learning models. With cross-validation, we still have our holdout data, but we use several different portions of the data for validation rather than using one fifth for the holdout, one for validation, and the remainder for training

All in all, a model is generally provided with a set of known data, called the training data set, and a set of unknown data to be tested against the model, known as the test data set, for a prediction problem. The goal is to have a data set in the training phase to test the model and then provide insight into how the particular model adapts to an independent data set. A round of cross-validation involves partitioning information into additional subsets and then performing an assessment on a single subset. After that, on other subsets (exam sets) the assessment is validated.

Many runs of cross-validation are conducted using many distinct partitions to decrease variation and then an average of the outcomes is obtained. Cross-validation is a strong method of estimating the performance technique of the model.

### Why Do We Need Cross-Validation?

The aim of cross-validation is to evaluate how an unidentified dataset conducts or behave on our prediction model. We will look at it from the point of perspective of a layman.

You’re learning to ride a vehicle. Now, on an vacant highway, anyone can ride a vehicle. In challenging traffic, the true experiment is how you ride. That’s why the trainers train you on traffic-intensive highways to get you used to it.

So when it’s truly time for you to ride your vehicle, you’re ready to do that without the trainer seated next to you to guide you. You are prepared to deal with any scenario you may not have experienced before.

#### Goal

In this blog, we will be learning about different cross-validation techniques and will be implementing them in R. For this purpose, we will train a simple regression model in R. We will be evaluating its performance through different cross-validation techniques

#### Data Generation

In this section, we will generate data to train a simple linear regression model. We will evaluate our linear regression model with different cross-validation techniques in the next section.

1 2 3 4 5 |
gen_sim_data = function(size) { x = runif(n = size, min = -1, max = 1) y = rnorm(n = size, mean = x ^ 2, sd = 0.15) data.frame(x, y) } |

### Different Types of Cross-Validation

#### 1. The Validation set Approach

A validation set is a collection of information used to train artificial intelligence in order to find and optimize the finest model for solving a particular issue. Also regarded as dev sets are validation sets. The bulk of the complete information is made up of training sets, approximately 60 per cent. In experimentation, in a method recognized as changing weights, the designs suit parameters.

The validation set represents approximately 20 per cent of the majority of the information used. In contrast to teaching and test sets, the validation set is an intermediate stage used to select and optimize the finest template.

Testing sets create up 20% of the data’s bulk. These sets are perfect information and outcomes for verifying an AI’s proper procedure. The test set is assured, usually through human verification, to be the input information clustered together with checked right inputs. This perfect environment is used to measure outcomes and evaluate the final model’s output.

1 2 3 4 5 |
set.seed(42) data = gen_sim_data(sample_size = 200) idx = sample(1:nrow(data), 160) trn = data[idx, ] val = data[-idx, ] |

1 |
fit = lm(y ~ poly(x, 10), data = trn) |

#### 2. Leave out one cross-validation

Leave-one-out cross-validation is K-fold cross-validation that takes the number of data points in the set to its logical extreme, with K equal to N. That implies that N is instructed on all information separately, the feature approximator except for one stage, and a forecast is created for that stage. As before the model is calculated and used to assess the median mistake. The assessment provided by a cross-validation error leave-one-out (LOO-XVE) is nice, but it seems very costly to calculate at first glance. Locally weighted learners can, fortunately, create LOO predictions as readily as periodic projections are made. This asserts that computing the LOO method uses no more time than computing the error. Also, it is a much better way to evaluate models.

1 2 3 4 5 6 7 |
# Define training control train.control = trainControl(method = "LOOCV") # Train the model model = train(Fertility ~., data = swiss, method = "lm",trControl = train.control) # Summarize the results print(model) |

#### 3. k-fold cross validation

One way to enhance the holdout technique is through K-fold cross-validation. The data set is split into k subsets and recurring k times are the holdout technique. One of the k subsets is used as the test set each time, and the other k-1 subsets are put together to form a training set. Then the median mistake is calculated in all k tests. The benefit of this technique is that how the information is split is less important. Each data point becomes precisely once in a test set and becomes k-1 times in a training set. Like “k” increases, the variance of the subsequent estimate is decreased.

The advantage of doing this is the independent selection of how large each test set is and how many trials one can average over.

1 2 3 4 5 6 7 8 |
### KFOLD # Define training control set.seed(123) train.control = trainControl(method = "cv", number = 10) # Train the model model = train(y ~., data = sim_data, method = "lm",<br /> trControl = train.control) # Summarize the results print(model) |

#### 4. Adversarial validation

In terms of feature distribution, the general idea is to check the degree of similarity between training and testing: if they are difficult to distinguish, the distribution is likely to be similar and the usual validation techniques should work. It doesn’t seem like that, so we can assume that they’re quite distinct. Combining train and sample sets, assigning 0/1 tags (0 — train,1-test) and assessing a binary classification assignment can quantify this assumption.

#### 5. Stratified k-fold cross validation

Stratification is the information rearrangement method to guarantee that each layer is a healthy representative of the whole. For example, in a binary classification issue where each class consists of 50 per cent of the data, it is best to arrange the data so that each class consists of about half of the instances in each fold. The intuition behind this concerns the bias of most algorithms for classification. They tend to weigh each example similarly, meaning that over-represented categories get too much weight (e.g. optimizing F-measure, accuracy, or an additional type of mistake). For an algorithm, stratification is not so essential that weights each category similarly

1 2 3 4 5 6 7 |
data(labels); examples.index = 1:nrow(L); examples.name = rownames(L); positives=which(L[,3]==1); x1 = do.stratified.cv.data.single.class(examples.index, positives, kk=5, seed=23); x2 = do.stratified.cv.data.single.class(examples.name, positives, kk=5, seed=23); <br />x3 = do.stratified.cv.data.over.classes(L, examples.index, kk=5, seed=23); x4 = do.stratified.cv.data.over.classes(L, examples.name, kk=5, seed=23); |

### Limitations of Cross-Validation

We have learnt about cross-validation in machine learning is and understood the importance of the concept. Although it is an important aspect of machine learning, it has its own limitations.

- Cross-validation is only meaningful until the time world represented by data is perfect. If there are anomalies in data then predictive modelling fails to perform well

- Let us consider an example where cross-validation can fail. Suppose we develop a model for predicting the one’s risk of suffering from a particular disease. However, if we train our model using data involving a specific section of the population. The moment we apply the model to the general population, the results may vary a lot.

### Summary

In this blog, we had a look at different cross-validation techniques. We also touched upon the implementation of these techniques through the code in R. Cross-validation is a crucial concept in increasing the ML model’s ability to perform well on unseen data. These techniques help us to avoid underfitting and overfitting while training our ML models and hence should not be overlooked!

**Follow this link**, if you are looking to **learn data science online!**

You can **follow this link for our Big Data course!**

Additionally, if you are having an interest in **learning Data Science, click here to start the Online Data Science Course**

Furthermore, if you want to read more about data science, read our **Data Science Blogs**