2
$\begingroup$

I am using $k$ fold cross validation, and I wonder how it can detect overfitting problem or how it can prevent the overfitting issue ?

Suppose I split the data into training and test set, applied CV on the training set.

Say, I got a mean cross validation score of $0.96$. But it fails to perform well on the test set, because the feature value present in the training set (say, gender : MALE) is completely not present in the test set (say, gender : FEMALE).

Q1. How can I say CV always detects the overfitting ?

Q2. Should I train the model using the training set after cross validation ?

Q3. What is a good cross validation score to say the model is not overfitted ?

$\endgroup$
1
  • $\begingroup$ "But it fails to perform well on the test set, because the feature value present in the training set (say, gender : MALE) is completely not present in the test set (say, gender : FEMALE)." that suggests your dataset may be too small to usefully train a model. A basic assumption of modelling is that the training and test sets have similar statistical distributions. $\endgroup$ Commented Jul 20, 2023 at 12:04

1 Answer 1

1
$\begingroup$

The textbook answer you'll see to this is something along the lines of "if there is a large gap between your training and test loss, you have overfit", where of course how large "large" has to be is a little unclear. Personally I think the truth is much more subtle than this.

I think probably the scenario in which you can see overfitting happening in the most transparent way is if you take your data and do a train/test split, and then train decision trees of ever-decreasing min_samples_leaf (or for something more discrete, ever-increasing max_depth). You'll see your training loss getting better and better until eventually it's perfect as the tree bottoms out into pure nodes, but your test loss will be optimal at some point in the middle. At min_samples_leaf sizes lower than the one which is optimal for your test set or equivalently max_depths higher than the optimal one, you have overfit. This is probably the only scenario I can think of in which there's a binary yes/no answer to whether your model has overfit, because you know the best you can possibly do with this data+model combination and you've introduced a higher level of complexity than optimal.

You will in this case probably find that at this optimal point, the delta between train and test loss is very low and as you surpass this optimal point, this delta starts to grow very quickly as the train loss continues to improve and the test loss gets worse. However, a textbook example of where this isn't true is random forests. Imagine doing the same experiment as above but with a random forest. You will find for the same dataset, the optimal point in min_samples_leaf or max_depth will occur at a point of higher model complexity than it does for the decision tree (and the optimal test performance will be better). Often you'll find, quite astonishingly, that the best test-performance (if you have enough trees) is occurring at min_samples_leaf=1 or 2. Your train loss will be almost perfect and your test loss will certainly not be perfect, but this might be the best you can get your test loss. Have you overfit? Well you've intentionally overfit the individual trees, but has the overall random forest model overfit? That's a philosophical question imo.

If you want to get more philosophical, I would say that as soon as you start to fit a model, you are starting to overfit. It's just that to begin with, you're mainly picking up signal + a bit of noise on that signal and as you continue, the rate at which you pick up signal starts to decrease and at some point in order to pick up any more signal, you pick up so much noise as to make the net effect a negative one. So all models are a somewhat overfit, the point of cross-validation is to pick the model which trades off overfitted- and underfittedness optimally on unseen examples.

So to attempt to answer your three questions based on what I have just said:

  1. Cross validation doesn't detect overfitting, it helps you find the model which best trades off over- vs underfitting and thus helps you find the model which is likely to perform best on unseen data

  2. I think your second question isn't really related to the rest of what you asked/what I said, but it's a common enough question. Best practice is to use CV to find the best model parameters/architecture and then use that setup and retrain on all data you have available

  3. There is no such thing. A good score generally depends on the problem, but I think maybe you meant "what is a good gap between train and test loss", to which there also isn't really an answer. Use CV to find the best possible performance on a test set which is an estimator for how well you'll do on unseen data in production, and then decide whether that score is good enough for what you're trying to do (usually by evaluating the costs of False Positives and Negatives Vs the benefit of True Positives and Negatives)

$\endgroup$
14
  • $\begingroup$ Given my example, the cross validation score is really good, but the model is still overfitted ?. Am I right?? $\endgroup$ Commented Jul 20, 2023 at 10:08
  • $\begingroup$ To be honest, I don't fully understand what you mean by a cross validation score of 0.96. There are two numbers you should be reporting, your train loss and your test loss. It sounds like 0.96 is your train loss ? (as an aside, what kind of model are you using?) $\endgroup$
    – gazza89
    Commented Jul 20, 2023 at 11:27
  • $\begingroup$ "I would say that as soon as you start to fit a model, you are starting to overfit. " is not correct. Overfitting means improving the fit to the training sample in a way that decreases generalisation performance. While you are improving generalisation performance, then by definition you are not overfitting. The fact that you can have an underfit model shows that not all models overfit from the start. How can you have a model that is simultaneously both under- and over-fitted? $\endgroup$ Commented Jul 20, 2023 at 11:27
  • $\begingroup$ "Cross validation doesn't detect overfitting" this isn't true either. If as you train the model or tune the hyper-parameters the cross-validation error gets worse while the training error gets better, you have detected overfitting. That is why early stopping can be effective. It doesn't always detect it with 100% reliability, but it does detect over-fitting most of the time. Note you can over-fit the cross-validation error as well if you tune too much. $\endgroup$ Commented Jul 20, 2023 at 11:31
  • $\begingroup$ The thing understand about k fold cross validation is we split the data in k folds then train the data iteratively k times with k-1 folds and leave 1 for validation. It is still not clear for me how it can say about overfitting ? I am using sklearn cross_val_score to find out the score of cross validation for each iteration. It is returning an array of the scores. What is the point of using cross validation on a data ? Please explain, from morning I am torning my hairs to find out these answers. $\endgroup$ Commented Jul 20, 2023 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.