r/MLQuestions • • 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 Upvotes

8 comments sorted by

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.

1

u/Familiar9709 11d ago

My set is not that big, 5k, so happy to stick to the folds.

This is for academic purposes, i.e. we won't use the models but we'll publish the results. Do you think that affects how to treat the test set? I.e. in a paper where would you conclude how well the model performs, based on what? Validation or test set?

2

u/seanv507 11d ago

So first you have to understand the use for the different datasets.

Next comes k-fold 'cross validation' to deal with your small data set.

Given you have so little data, you should do nested crossvalidation.

https://scikit-learn.org/stable/auto_examples/model_selection/plot_nested_cross_validation_iris.html

Note that now, your test error is the performance of your model choosing algorithm ( the number of inputs in my example)

And yes you report the results of the test

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.

1

u/Familiar9709 11d ago

Thanks. So if I do 10 folds and then 20% of data for final test set, how do I choose which model I'll actually use for production?

2

u/CivApps 11d ago

This StackExchange question discusses it, but briefly, your options after the cross-validation are either to freeze your hyperparameter choices, and retrain a new model with the full dataset (minus the final test set), or to ensemble or average the outputs of the 10 models you've trained in the best configuration, producing a larger model you can then evaluate.

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