What Is Overfitting? A Measured Example on 12,330 Shopping Sessions
Overfitting is when a model learns its training rows instead of the pattern. Measured: a decision tree scored 100% on training rows and 71% on rows it had never seen.
In this article
Overfitting is when a model learns its training rows instead of the pattern behind them. An overfitted model scores very well on the data it was built from and noticeably worse on new data, which is the only data that matters once it is in use. The way to see it is to score the model on rows it never saw: held-out evaluation.
Measured example: on 12,330 public online-shopping sessions, a decision tree allowed to grow without limit scored 100% balanced accuracy on its training rows and 71.1% on 2,466 held-out rows. A tree limited to three levels scored 85.6% on training rows and 84.3% held out. The bigger model looked perfect and was worse.
How overfitting happens
A model has a certain amount of freedom: how many rules, branches or weights it can use. With little freedom it can only learn broad patterns. With a lot of freedom it can also learn the accidents of one particular sample: a customer who happened to buy after exactly seven product pages, a quirk in one month's traffic. Those accidents do not repeat in new data, so the rules built on them make wrong predictions there.
The training score cannot show this, because the accidents are real in the training rows. Only rows the model never saw can.
A measured example: decision trees on shopping sessions
We used the public Online Shoppers Purchasing Intention dataset from the UCI Machine Learning Repository: 12,330 website sessions, each with page counts, time on page, bounce and exit rates, and whether the session ended in a purchase. We held back 20% of the sessions (2,466, of which 382 bought), trained decision trees of increasing depth on the other 9,864, and scored every tree on both sets. Scores are balanced accuracy, so buyers and non-buyers count equally.
| Tree depth | Leaves (rules) | Training rows | Held-out rows | Gap |
|---|---|---|---|---|
| 1 | 2 | 84.9% | 83.3% | 1.6 |
| 3 | 8 | 85.6% | 84.3% | 1.3 |
| 5 | 30 | 86.9% | 84.4% | 2.5 |
| 8 | 121 | 88.9% | 83.5% | 5.4 |
| 12 | 364 | 93.1% | 80.0% | 13.1 |
| 20 | 803 | 98.4% | 74.1% | 24.3 |
| unlimited | 1,001 | 100.0% | 71.1% | 28.9 |
As the tree gets deeper, the training score climbs all the way to 100% while the held-out score falls. The gap between the two columns is the overfitting.
One trap: plain accuracy hides part of this. The unlimited tree's held-out accuracy was 85.4%, higher than the three-level tree's 83.3%, because most sessions end without a purchase and a model that says "no purchase" more often gets more of them right. Its balanced accuracy shows the cost: it finds fewer of the buyers. See Balanced accuracy vs. accuracy.
How to choose the right size without peeking
The held-out rows must not be used to choose the model; otherwise they stop being unseen. We chose the depth with 5-fold cross-validation on the training rows only: the training rows are split into five parts, each part is scored by a tree trained on the other four, and the depth with the best average wins. Cross-validation chose depth 3 (85.4% average), and that tree scored 84.3% on the held-out rows: close to what cross-validation predicted.
Choosing among many models can overfit too
Overfitting is not only about one model's size. If you build many models and keep the one that scores best on a set of rows, part of its winning score is luck on those particular rows. Its score on the rows used to choose it is optimistic.
YourCloudGroup Model Manager is built around this. On the same public shopping data, in its sample-data demonstration, it:
- built 12 models and held back 1,215 blind rows;
- chose the winner (a Random Forest) using half of the blind rows;
- certified it on the other 608 rows, which took no part in choosing it: 76.2% balanced accuracy against 50.0% for a baseline that always predicts the most common outcome, with a 90% range for the lift over the baseline.
On all 1,215 blind rows, including the half it was chosen on, the same winner scores 78.6% (recomputed from the run's downloadable predictions). On the untouched half alone it scores 76.2%. A gap of this size can go either way on a few hundred rows, which is why the certificate reports a range and is based only on rows that played no part in the choice.
The demonstration's model and split differ from our decision-tree experiment, so the two sets of figures should not be compared with each other.
How to tell if a model is overfitted
- Compare training and held-out scores. A large gap is the signature of overfitting.
- Score on rows the model never saw, and never use those rows to pick the model.
- Keep a final set of rows for the winner only when you compare many models.
- For dated data, hold out the latest rows, not a random sample, so the test looks like the future the model will face. See What is held-out model evaluation?
What this example does not show
One dataset and one model type. Deeper trees are not always worse; the right size depends on how much data you have and how much real pattern it contains. What carries over is the method: judge a model by rows it never saw.
Takeaway
Overfitting is a model learning its training rows instead of the pattern. Here a decision tree went from 84.3% to 71.1% on unseen sessions while its training score rose to 100%. A model's training score is not evidence of how it will do; a score on rows that took no part in building or choosing it is.
See it on sample data: Which website visitors will buy shows how Model Manager tested a model on the public Online Shoppers Purchasing Intention (UCI) dataset: the verdict, the measured figures and the predictions to download.
Sources
- Sakar, C. & Kastro, Y. Online Shoppers Purchasing Intention Dataset. UCI Machine Learning Repository. doi:10.24432/C5F88Q. CC BY 4.0.
- Experiment script:
overfitting_shoppers.py(stratified 80/20 split, seed 42, scikit-learn decision trees, depth chosen by 5-fold cross-validation on training rows). - YourCloudGroup Model Manager sample-data run: yourcloudgroup.com/try/shoppers (results and predictions downloadable there).
Build predictive models from your own data
YourCloudGroup Model Manager builds, evaluates, certifies and schedules predictive models from your CSV or spreadsheet data — point and click, on your own dedicated server.
Learn about Model Manager →Published by YourCloudGroup. Read our editorial standards and corrections policy.