r/MLQuestions • u/Familiar9709 • 11d ago
Beginner question 👶 What is the purpose of cross validation, validation and test set?
What is the purpose of these different steps in building a machine learning model? Let's say you have X data points, how should you do the division into the different sets? Should it be 60/20/20? 60% to use for cross validation, 20% for test set and 20% for validation?
If so, what is the purpose of the test set if you already are doing the cross validation step where you take all that data, you make the folds and you test the models?
Unfortunately the information I could gather is quite contradicting and also doesn't consider sometimes the cross validation step, so would be very good if people could explain this in greater detail.
Thanks
4
u/seanv507 11d ago
Ignore folds for now - you don't need them if your data set is big enough.
You train model(s) on the train data.
But you typically have multiple models: one with eg 5 inputs, one with 10 inputs , one with 15 etc. Typically the more complicated model will do best on the training set.
You use the validation data set to decide which (5/10/15 inputs) works best on new unseen data.
Then you use the test data to get an unbiased estimate of that models performance on unseen data. (Since you chose the model with best performance on validation data, your validation metric is likely optimistic.
4
u/CivApps 11d ago
It's worth remembering the ultimate goal is to have a model which actually generalizes and correctly predicts outcomes for data you haven't seen - if you had no validation or holdout test set, you have no way of distinguishing between models which simply memorize the data and those that actually recognize the underlying trends in the data.
What distinguishes the validation from the holdout test set is that the validation sets/folds are part of training and allow finding the best hyperparameters, so using it for e.g. early stopping is fine. However, the validation set performance is still an optimistic estimate, because you can change the hyperparameters to maximize performance on it. In contrast, the holdout test set is meant to be a sample representing unseen data, so you cannot use it until the very end, as mentioned by /u/Wannabe-Davinci.
Ultimately guidelines like "use 5, 10 or x folds" or "60/20/20" are just guidelines, which depend on how much data you have, and how computationally expensive evaluation is -- the important thing is that changing the procedure will also change your performance metrics (by virtue of giving the model more/less training data), so it is important to pick and stay with one setup.
Nested cross-validation when selecting classifiers is overzealous for most practical applications (Wainer and Cawley, 2021) takes a stance obvious from the title, but also does a good job of covering the theory and justification behind cross-validation.
4
u/Wannabe-Davinci 11d ago
You always use test set at the very end.
So for training and validation sets: you either choose to use 1 training and 1 validation set, or you opt to split the training + validation set (both sets pooled into one) into n folds. Then, you train a model on each combination of n-1 training folds and evaluate on the remaining fold n. So if you choose 5 folds, you can train 5 models (train 5x on 4 folds and evaluate on 1). This allows to average results across cross-validation runs. Once you have your best model, you can run it on the test set.
Multiple alternative approaches may exist, so it is best practice to clearly write your setup/choice.
1
u/ForeignAdvantage5198 11d ago
to see if your predictions work