What is overfitting in machine learning? Explained with examples

A model can score 100% in practice and still fail the real test. That's overfitting. Here is what it is, how to spot it, how it differs from underfitting, and the common ways to prevent it.

Two students. The first memorised the answers to the practice questions and scores 100% in practice but only 40% in the real exam. The second understood the ideas and scores 85% in practice and 82% in the exam. Labels: overfitting (memorised) and good fit (learned the pattern). Scores are illustrative.
Fig. 1 — Overfitting is memorising the practice paper instead of learning the subject. (Illustrative scores.)

Imagine a student who gets hold of last year’s practice paper and memorises every answer. In practice tests, they score 100%. Then the real exam arrives with new questions, and they score 40%. They didn’t learn the subject; they learned those particular questions.

AI models can make exactly the same mistake. It’s called overfitting, and it’s one of the most common problems in machine learning. This guide explains what overfitting is, how to spot it, how it differs from underfitting, and the everyday techniques people use to prevent it.

What is overfitting?

Overfitting is when a machine learning model learns its training examples too closely, including their noise and accidents, so it performs very well on the training data but badly on new data it hasn’t seen before.

The goal of machine learning is never to do well on the training examples themselves. The goal is to generalise: to learn patterns that also hold for new examples. An overfitted model has learned the details of its training set instead of the general pattern.

A simple example

Say you train a model to tell apples from oranges using photos. By chance, most apple photos in your training set were taken on a wooden kitchen table, and most orange photos on a white plate.

An overfitted model might learn “wood in the background means apple”. It will score brilliantly on your training photos. But show it an apple on a white plate and it says “orange”. It learned a coincidence in the data, not what an apple looks like.

This kind of accidental shortcut is common, and it’s one reason a good, varied dataset matters so much.

Overfitting vs underfitting

There are two ways to get it wrong, and one way to get it right.

Three charts of the same scattered dots, which roughly follow a gentle upward curve. Left, underfitting: a flat straight line that misses the trend. Middle, good fit: a smooth curve that follows the general trend. Right, overfitting: a wiggly line that passes exactly through every single dot, including the odd ones.
Underfitting misses the pattern. Overfitting memorises every dot, noise included. A good fit follows the trend.
  • Underfitting: the model is too simple, or hasn’t trained enough, to capture the pattern. It does badly on training data and on new data. Like a student who barely studied.
  • Good fit: the model captures the real pattern. It does well on training data and almost as well on new data.
  • Overfitting: the model is too closely tuned to the training data. It does very well on training data but noticeably worse on new data. Like the student who memorised the practice paper.

How do you spot overfitting?

The tell-tale sign is a gap between how well the model does on data it trained on and data it didn’t.

That’s why datasets are split into a training set, a validation set and a test set (see datasets). During training, you watch two numbers:

  • Training error: how wrong the model is on the examples it’s learning from.
  • Validation error: how wrong it is on examples it hasn’t learned from.

At first, both go down together. If training continues too long, training error keeps going down while validation error often stops improving or starts going up. That turning point is where the model has started memorising instead of learning.

Watch out for data leakage, which can hide overfitting: if test examples sneak into the training set, the scores look great and mean nothing.

Why does overfitting happen?

  • Too little data. With only a few examples, it’s easy to memorise them all.
  • A model that’s too complex for the task. A model with lots of adjustable numbers can fit almost anything, including noise.
  • Training for too long on the same examples.
  • Noisy or biased data, such as wrong labels or accidental patterns like the wooden table.

How to prevent overfitting

There’s no single cure, but these are the standard tools:

  1. Get more (and more varied) data. The more different examples a model sees, the harder it is to memorise them and the easier it is to find the real pattern.
  2. Data augmentation. Create extra training examples by changing existing ones slightly: flip, crop or brighten photos, or reword sentences.
  3. Keep a separate test set and never train on it. This doesn’t prevent overfitting by itself, but it’s how you catch it.
  4. Early stopping. Stop training when the validation error stops improving.
  5. Use a simpler model, or one with fewer adjustable numbers, when the data is small.
  6. Regularisation. Add a gentle penalty for complexity during training, so the model prefers simpler explanations.
  7. Dropout (for neural networks). During training, randomly switch off some neurons each time, so the network can’t rely too heavily on any single one.
  8. Cross-validation. Train and test several times on different splits of the data. It helps you detect overfitting and choose settings that generalise.

Does overfitting affect large language models?

Yes, in its own ways. Large language models are trained on so much text that classic overfitting is less of a problem during pre-training, but related issues still matter:

  • Memorisation. Models can memorise and repeat pieces of their training text, which raises privacy and copyright concerns.
  • Benchmark contamination. If test questions from a public benchmark ended up in a model’s training data, its score on that benchmark can look better than its real ability. This is a form of data leakage.
  • Fine-tuning overfits easily. Fine-tuning on a small set of examples can make a model great at those examples but worse at general tasks.

Common misconceptions

“A model that scores 100% on its training data is a great model.” Not necessarily. Perfect training scores are often a warning sign. Judge a model on data it hasn’t seen.

“Overfitting means the model is too smart.” It means the model learned the wrong thing: the details of its examples instead of the general rule.

“More training always makes a model better.” Past a point, more training on the same data can make it worse on new data.

“Only big models overfit.” Any model can overfit, especially when the dataset is small.

Key takeaways

  • Overfitting: a model does great on training data but badly on new data, because it memorised instead of learning the pattern.
  • Underfitting: a model is too simple to capture the pattern at all.
  • Spot overfitting by comparing training error with validation error on unseen data.
  • Common causes: too little data, too complex a model, training too long, noisy data. Watch out for data leakage, which hides overfitting.
  • Common fixes: more varied data, augmentation, early stopping, simpler models, regularisation, dropout, cross-validation.

Frequently asked questions

What is overfitting in simple words?

Overfitting is when an AI model memorises its practice examples instead of learning the general idea, so it does well on what it has seen and badly on anything new.

What is the difference between overfitting and underfitting?

Overfitting means the model fits the training data too closely, noise included, and fails on new data. Underfitting means the model is too simple to capture the pattern, so it does badly on both training and new data.

How do I know if my model is overfitting?

Compare its performance on training data with its performance on a separate validation or test set. A big gap, with training much better, is the classic sign.

Can more data fix overfitting?

Often, yes. More varied, good-quality data makes it harder for the model to memorise and easier to learn the real pattern. It’s usually the most reliable fix when you can get it.

What is early stopping?

Early stopping means ending training at the point where the model’s performance on validation data stops improving, before it starts memorising the training examples.

Comments

LEAVE A COMMENT
No sign-up. Comments appear after a quick review.