The main methods for validating a model are the holdout method (a train, validation and test split), k-fold cross-validation, and time-based splitting for data that has a time order. Each one estimates how the model will perform on data it has not seen, and each suits different kinds of data.
Why you need data the model has not seen
If you score a model on the same examples it learned from, the result is usually too optimistic. The model may have memorized details of those examples instead of learning patterns that carry over. Validation uses data held back from training to give a fairer estimate.
Holdout validation
Split the dataset once into three parts:
- Training set: used to fit the model.
- Validation set: used to compare settings and choose between models.
- Test set: used once, at the end, to report a final score.
For example, you might keep 80% of the data for training and divide the rest between validation and test. The right split depends on how much data you have. With a small dataset, one split can give an unlucky estimate, which is one reason cross-validation exists.
K-fold cross-validation
Split the data into k roughly equal parts, called folds. Train the model k times. Each time, hold out a different fold for testing and train on the others. Then average the k scores. Common choices are 5 or 10 folds.
Cross-validation makes better use of small datasets and shows how much scores vary from one split to the next. The cost is time, since the model is trained k times.
Stratified k-fold keeps the class proportions similar in each fold. It helps when one class is much rarer than the others.
Leave-one-out is an extreme case where k equals the number of examples. It can work for very small datasets, but it is slow on larger ones.
Time-based validation
For data with a time order, such as sales records, sensor readings or prices, a random split can let information from the future leak into training. Instead, train on earlier data and test on later data. Walk-forward validation repeats this across several time windows, so the model is checked over more than one period.
Metrics to record
Pick metrics that match the problem:
- Classification (predicting a category): accuracy, precision, recall, F1 score and ROC AUC. Accuracy can look good even when a rare class is missed, so check the others too.
- Regression (predicting a number): mean absolute error, mean squared error and R-squared.
Also compare training and validation scores. A large gap, where training looks much better, often points to overfitting, meaning the model has learned the training data too closely.
Keep validation results with the model
A validation score only means something next to the model version and settings that produced it. Record the method, the split or fold settings, the random seed and the metric values with each version. Storing these results with each model is part of version control machine learning models, because a score kept in a spreadsheet is easy to lose or match to the wrong run.
Summary
Holdout validation is the simplest method, k-fold cross-validation gives a steadier estimate when data is limited, and time-based splits suit data with an order. Choose the method that matches your data, pick metrics that match your problem, and keep the results with the version they describe.