Overview
Cross Validation is used when the dataset is not very large and you cant afford to lose any data points in the learning process. Cross Validation splits the dataset in k equal sized datasets (or nearly equal). Then the model is trained k times. Each time, one of the k sets is used as the test set, and the other sets are used to train the model.
Sample Implementation
The following algorithm shows splitting a dataset into different sets of length equal to size.
let size = 10;
let sets = [];
let set = [];
for(let i=0;i<data.length;i++){
if(i%size == 0){
set = [];
sets.push(set);
}
set.push(data[i]);
}
An extreme version of this algorithm is to create test sets of size 1. That is, the model is trained on all the points of the dataset except one and then tested on the one removed point. This is done for each point in the dataset.