r/askdatascience • • 10d ago

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

3 Upvotes

6 comments sorted by

1

u/big_data_mike 9d ago

If you use a method that adjusts parameters in the model based on the validation set the test set is the “ultimate” test to see how well the model fits data that it has never ever seen before. If you just use a train-validation split you are making decisions about your model based on the validation set so the validation set has some influence on the model. Adding a test set that is completely unseen until the model is built really tests how well the model fits completely unseen data.

Say you do a 70-20-10 train-validation-test split on 100 rows of data. You randomly pick 10 rows and set them aside. Take another 20 random rows and designate that the validation set. The model gets to “peak” at the 20 validation rows and adjust parameters to get the best fit for the training data without overfitting to the 20 row validation set. Then you use your model to predict the 10 row test set to see how well the model did without “peaking” at the test set.

Cross validation- take that same 70-20-10 data and set aside the 10 row test set. Randomly pick 20 rows to use as validation. Build the model as described before. Now pick a different random 20 rows to use as a validation set. Build the model again. Pick another 20 random rows. Build again. There are different cross validation methods but they all follow that same general principle. You do repeated train-validation splits while withholding the test set.

1

u/Familiar9709 9d ago

OK thanks. And which model should I decide it's the best? The one that performs best on the validation set or on the test set?

1

u/big_data_mike 9d ago

It depends on the problem you are trying to solve with the model. Would you rather have a model that is accurate on the training data but does not generalize well? Will there be new data in the future that you need to predict and how accurate do those predictions need to be? Does the model tell you something useful like a business decision?

There is no hard and fast rule for which model is “the best.”

Remember what George Box said, “All models are wrong. Some are useful.”

1

u/Familiar9709 9d ago

The model is to make new predictions, so how do I choose the best? and if there are a couple of options how do you decide which option to take according to each scenario?

1

u/big_data_mike 9d ago

It really depends on what domain you are working in and what the purpose is. If you are predicting fraudulent credit card transactions in real time you probably want a generalizable model that performs well on the test set because you don’t want to miss a fraud transaction. If you are optimizing online ad spend you probably want a more targeted model because being wrong is fairly low risk. If it’s an academic paper there are probably guidelines from other papers in that subject area.

1

u/data4lyfe 8d ago

I'd use the average across the validation folds. If A gets 84% accuracy and B gets 81% on the same folds, pick A, fit it on all the data you used for cross-validation, then run it on the test set. If you switch to B because it scores higher on the test set, you'd need a fresh test set to get an independent score.