KAIROS CODERS

Overfitting vs Underfitting in Machine Learning

user

Rahul

August 19, 2026 at 02:53 PM

View Count: 8

Overfitting vs Underfitting in Machine Learning

Imagine you are preparing for an exam.

One student memorizes every answer from a practice paper but struggles when the questions are slightly different.

Another student barely studies and cannot solve even the basic questions.

Then there is a third student who understands the concepts well enough to solve both familiar and unfamiliar questions.

Machine Learning models can behave in a very similar way.

A model that memorizes the training data too closely is suffering from overfitting.

A model that is too simple to learn the important patterns is suffering from underfitting.

The goal is to build a model that learns the underlying patterns well enough to perform accurately on new, unseen data.

This ability is called generalization.


Table of Contents

  1. What Is Overfitting?
  2. What Is Underfitting?
  3. What Is Good Generalization?
  4. A Simple Real-World Analogy
  5. Overfitting vs Underfitting
  6. Training Error vs Validation Error
  7. Why Overfitting Happens
  8. Why Underfitting Happens
  9. How to Fix Overfitting
  10. How to Fix Underfitting
  11. The Bias-Variance Tradeoff
  12. Model Complexity
  13. Regularization
  14. Cross-Validation
  15. Early Stopping
  16. Data Augmentation
  17. Real-World Examples
  18. Frequently Asked Questions
  19. Key Takeaways
  20. Conclusion

What Is Overfitting?

Overfitting occurs when a Machine Learning model learns the training data too closely, including noise and accidental patterns, causing it to perform poorly on new data.

The model may achieve extremely high training accuracy but much lower validation or test performance.

For example:

Training Accuracy: 99%

Validation Accuracy: 72%

 

This is a classic warning sign.

The model appears excellent during training but fails to generalize.


A Simple Example of Overfitting

Imagine a student memorizes:

Question 1 → Answer A
Question 2 → Answer C
Question 3 → Answer B

The student performs perfectly when given the exact same questions.

But when the teacher changes the wording, the student struggles.

The student learned the answers, not the concepts.

An overfitted Machine Learning model behaves similarly.


What Is Underfitting?

Underfitting occurs when a Machine Learning model is too simple or insufficiently trained to capture the important patterns in the data.

The model performs poorly on both training and unseen data.

For example:

Training Accuracy: 65%

Validation Accuracy: 63%

 

The model hasn't learned enough.


A Simple Example of Underfitting

Imagine a student preparing for a mathematics exam but studying only one formula.

When the exam contains different types of problems, the student cannot solve them.

The problem isn't memorization.

The problem is that the student hasn't learned enough.

That's similar to underfitting.


What Is Good Generalization?

The ideal model sits between these two extremes.

It should learn meaningful patterns from the training data without memorizing noise.

For example:

Training Accuracy: 91%

Validation Accuracy: 89%

Test Accuracy: 90%

 

The exact numbers aren't the important part.

The important thing is that performance remains reasonably consistent on unseen data.


The Three Scenarios

Think of Machine Learning model complexity like this:

Too Simple

    ↓

Underfitting

Good Complexity

    ↓

Good Generalization

Too Complex

    ↓

Overfitting

 

The objective is to find the sweet spot.


Overfitting vs Underfitting

CharacteristicUnderfittingGood FitOverfitting
Model complexityToo lowAppropriateToo high
Training performancePoorGoodExcellent
Test performancePoorGoodPoor
Learns patternsNot enoughUseful patternsPatterns + noise
GeneralizationPoorGoodPoor

Training Error vs Validation Error

One of the easiest ways to identify these problems is to compare training and validation performance.

Underfitting

Training Error   → High

Validation Error → High

 

The model isn't learning enough.

Good Fit

Training Error   → Low

Validation Error → Low

 

The model generalizes reasonably well.

Overfitting

Training Error   → Very Low

Validation Error → High

 

The model performs much better on training data than unseen data.


Visualizing Model Complexity

Imagine increasing model complexity gradually.

Model Complexity →

 

At the beginning:

Training Error: High

Validation Error: High

 

As complexity increases:

Training Error ↓

Validation Error ↓

 

Eventually, the model reaches a useful level of complexity.

But if complexity continues increasing:

Training Error ↓↓↓

Validation Error ↑

 

The model starts overfitting.


Why Does Overfitting Happen?

There are several common causes.


1. Model Is Too Complex

A highly flexible model can learn extremely detailed patterns.

If the dataset is small, the model may start learning noise.

For example:

Small Dataset

+

Very Complex Model

=

Higher Risk of Overfitting

 


2. Too Little Training Data

Imagine trying to understand customer behavior using only 20 customers.

A model might learn strange patterns specific to those individuals.

More representative data can help the model identify broader patterns.

However, simply increasing the number of examples isn't always sufficient if the additional data is low quality or unrepresentative.


3. Noisy Data

Real-world datasets often contain:

  • Measurement errors
  • Incorrect labels
  • Random fluctuations
  • Outliers

A highly flexible model may accidentally learn these irregularities.


4. Too Many Features

Suppose you have:

10,000 Features

 

but only:

500 Training Examples

 

The model may have far more opportunities to discover accidental relationships.

This can increase overfitting risk.


5. Training for Too Long

In some neural network training scenarios, a model may initially improve on both training and validation data.

Eventually:

Training Loss ↓

Validation Loss ↑

 

This can indicate that training has continued beyond the point of best generalization.


Why Does Underfitting Happen?

Underfitting has different causes.


1. Model Is Too Simple

Suppose you're trying to predict house prices using only:

House Size

 

But house prices also depend heavily on:

  • Location
  • Bedrooms
  • Property age
  • Amenities
  • Market conditions

A very simple model may fail to capture the relationships in the data.


2. Insufficient Training

A model may simply not have had enough training to learn useful patterns.

This can happen with neural networks and other iterative learning algorithms.


3. Poor Features

If the available features don't contain enough useful information, the model may struggle regardless of algorithm choice.

This is sometimes described as an underpowered representation of the problem.


4. Excessive Regularization

Regularization helps prevent overfitting.

But if it is too strong, it can make the model overly constrained.

That can lead to underfitting.


How to Fix Overfitting

There are several strategies for reducing overfitting.


1. Collect More Relevant Data

More representative training data can help the model learn general patterns instead of memorizing a small dataset.

For example:

Before:

5,000 examples

After:

100,000 diverse examples

 

The additional data can provide broader coverage of real-world conditions.


2. Simplify the Model

Reduce unnecessary complexity.

Depending on the model, this could mean:

  • Fewer parameters
  • Shallower trees
  • Fewer features
  • Simpler architecture

The objective is to use enough capacity to solve the problem without unnecessary complexity.


3. Remove Irrelevant Features

Feature selection can reduce noise.

For example, if a feature has no meaningful relationship with the target, removing it may improve generalization.


4. Use Regularization

Regularization discourages models from becoming unnecessarily complex.

Common techniques include:

  • L1 regularization
  • L2 regularization
  • Weight decay

5. Use Dropout

Dropout is commonly used in neural networks.

During training, some neurons are randomly deactivated.

Conceptually:

Normal Network

     ↓

Some Connections Temporarily Disabled

     ↓

Network Learns More Robust Representations

 

Dropout can reduce reliance on specific pathways and help reduce overfitting in some neural network architectures.


6. Early Stopping

During training, monitor validation performance.

For example:

Epoch 1 → Validation Loss: 0.80

Epoch 10 → Validation Loss: 0.42

Epoch 20 → Validation Loss: 0.30

Epoch 30 → Validation Loss: 0.28

Epoch 40 → Validation Loss: 0.35

 

The model improved until around Epoch 30 and then started getting worse.

Early stopping can stop training around the point where validation performance is best.


7. Data Augmentation

Data augmentation creates variations of existing training examples.

This is particularly common with image data.

For example:

Original Image

   ↓

Rotate

   ↓

Crop

   ↓

Flip

   ↓

Brightness Adjustment

 

The model sees more variations and may become more robust.

Augmentation must be appropriate for the task. Some transformations can change the meaning of an example.


How to Fix Underfitting

Now let's look at the opposite problem.


1. Increase Model Complexity

A more expressive model may be able to capture relationships that a simpler model misses.

For example:

Linear Model

       ↓

More Flexible Model

 

But increasing complexity should be guided by validation performance rather than complexity for its own sake.


2. Add Better Features

Feature engineering can provide more useful information.

Instead of:

Purchase Amount

 

you might derive:

Average Purchase Amount

Purchase Frequency

Days Since Last Purchase

 

depending on the task.


3. Train Longer

If the model has not learned enough, additional training may improve performance.

However, training should be monitored carefully because continuing indefinitely can eventually increase overfitting.


4. Reduce Excessive Regularization

If regularization is too strong, relaxing it can allow the model to learn more complex patterns.


5. Improve Data Representation

Sometimes the issue isn't the algorithm.

The input representation itself may not contain the information the model needs.

Better preprocessing, feature extraction, or learned representations can help.


What Is the Bias-Variance Tradeoff?

Overfitting and underfitting are closely related to two important concepts:

  • Bias
  • Variance

High Bias

A high-bias model makes strong simplifying assumptions.

It may fail to capture important relationships.

This is commonly associated with underfitting.

High Bias

   ↓

Model Too Simple

   ↓

Underfitting

 


High Variance

A high-variance model is highly sensitive to the particular training dataset.

It may fit training examples extremely closely but perform poorly on new data.

This is commonly associated with overfitting.

High Variance

     ↓

Model Too Sensitive

     ↓

Overfitting

 


The Bias-Variance Tradeoff

The challenge is to find a balance.

High Bias

    ←──────────────→

High Variance

Underfitting       Overfitting

 

The ideal model balances both well enough to generalize.


Model Complexity and the Bias-Variance Tradeoff

As model complexity increases:

Bias ↓

Variance ↑

 

As model complexity decreases:

Bias ↑

Variance ↓

 

This is a simplified conceptual relationship. Real-world behavior can be more complicated, especially with modern deep learning systems.


What Is Regularization?

Regularization is a collection of techniques designed to reduce overfitting by discouraging overly complex solutions.


L1 Regularization

L1 regularization adds a penalty related to the absolute values of model weights.

It can encourage some weights toward zero.

This can sometimes produce sparse models.


L2 Regularization

L2 regularization penalizes large weights.

It encourages the model to distribute information rather than relying excessively on very large individual parameters.


Cross-Validation

Cross-validation is a technique used to evaluate how well a model is likely to generalize.

One common approach is K-Fold Cross-Validation.

The dataset is divided into several parts.

For example:

Fold 1 → Validation

Fold 2 → Training

Fold 3 → Training

Fold 4 → Training

Fold 5 → Training

 

Then the process rotates:

Fold 2 → Validation

Fold 1 → Training

Fold 3 → Training

...

 

The model is evaluated multiple times.

This can provide a more robust estimate of performance, particularly when datasets are relatively small.


Why Cross-Validation Helps

Suppose your dataset contains only 1,000 examples.

A single train/validation split may accidentally produce an unusually easy or difficult validation set.

Cross-validation allows multiple subsets of the data to participate in validation.

This can provide a more reliable estimate of how the model behaves across different samples.


Overfitting in Decision Trees

Decision Trees provide a simple example.

A shallow tree might be:

Age?

 ↓

Income?

 ↓

Prediction

 

A very deep tree might continue splitting until it creates extremely specific rules for individual training examples.

The deep tree could achieve:

Training Accuracy → 100%

 

while performing poorly on new data.

Techniques such as:

  • Limiting tree depth
  • Minimum samples per leaf
  • Pruning

can help control complexity.


Overfitting in Neural Networks

Neural networks can contain millions or even billions of parameters.

A sufficiently large network can potentially memorize training examples.

Common strategies for improving generalization include:

  • More relevant data
  • Data augmentation
  • Weight decay
  • Dropout
  • Early stopping
  • Transfer learning
  • Careful evaluation

Modern neural networks can sometimes generalize surprisingly well even when they have enormous numbers of parameters, so model size alone does not determine whether overfitting will occur.


Real-World Example: House Price Prediction

Imagine training a model with 100 houses.

The model discovers:

Large houses → Higher prices

 

That's a useful pattern.

But suppose it also learns:

Houses photographed on sunny days → Higher prices

 

If that relationship is just a coincidence in the training data, learning it would be undesirable.

That's an example of the model learning noise or accidental correlations.


Real-World Example: Medical AI

Suppose an image classification model is trained to identify a disease.

The training images from one hospital mostly contain a particular scanner watermark.

The model might accidentally learn:

Watermark → Disease

 

rather than learning the actual medical characteristics.

The model may perform well on the original dataset but fail when images come from another hospital.

This illustrates why dataset diversity, careful evaluation, and external validation matter.


Real-World Example: Fraud Detection

Suppose a fraud detection model is trained using historical transactions.

If fraudsters change their behavior, the old patterns may no longer work.

A model could therefore become less effective over time even if it was originally well trained.

This isn't necessarily traditional overfitting alone—it can also involve data distribution changes and concept drift.


How Do You Know If a Model Is Overfitting?

Watch the relationship between training and validation performance.

A common pattern is:

Training Performance ↑

Validation Performance ↑

 

Good.

Then:

Training Performance ↑

Validation Performance ↓

 

Potential overfitting.


How Do You Know If a Model Is Underfitting?

If both training and validation performance remain poor:

Training Performance → Poor

Validation Performance → Poor

 

the model may be underfitting.

But always investigate other possibilities too, such as:

  • Poor data quality
  • Incorrect labels
  • Distribution mismatch
  • Weak features
  • Optimization problems

The Goal of Machine Learning

The ultimate objective isn't to achieve the highest possible training accuracy.

It's to build a system that performs reliably on new, unseen data.

Think of the journey as:

Training Data

      ↓

Learn Patterns

      ↓

Validate

      ↓

Tune Model

      ↓

Test

      ↓

Generalize

      ↓

Real-World Predictions

 


Frequently Asked Questions

What is overfitting?

Overfitting occurs when a model learns training data too closely and performs poorly on unseen data.

What is underfitting?

Underfitting occurs when a model is too simple or insufficiently trained to capture important patterns.

Which is worse: overfitting or underfitting?

Neither is universally worse. Both indicate poor generalization, and the appropriate solution depends on why the model is failing.

How can overfitting be reduced?

Common approaches include more relevant data, regularization, simpler models, feature selection, data augmentation, and early stopping.

How can underfitting be fixed?

Consider a more expressive model, better features, additional training, improved representations, or less aggressive regularization.

What is generalization?

Generalization is the ability of a model to perform well on data it did not see during training.

What is the bias-variance tradeoff?

It describes the tension between overly simplistic models that have high bias and overly sensitive models that have high variance.

Does a larger AI model always overfit?

No. Model size alone does not determine generalization. Data quantity and quality, architecture, training procedures, regularization, and many other factors matter.


Key Takeaways

  • Overfitting means the model learns training data too closely.
  • Underfitting means the model fails to learn enough useful structure.
  • The goal is good generalization.
  • Training performance alone isn't enough to evaluate a model.
  • Validation and test data help measure performance on unseen examples.
  • Regularization, early stopping, feature selection, and data augmentation can help reduce overfitting.
  • Better features, more expressive models, and sufficient training can help address underfitting.
  • The bias-variance tradeoff provides a useful framework for understanding model complexity.
  • Real-world distribution changes can cause performance problems even when a model initially generalizes well.

Conclusion

Building a Machine Learning model isn't simply about choosing the most powerful algorithm and training it on as much data as possible.

The real challenge is finding the right balance.

A model that is too simple may fail to understand the problem.

A model that is too flexible may memorize the training data.

The best models learn useful patterns that continue to work when the data changes.

That's why Machine Learning engineers spend so much time evaluating models on unseen data, tuning complexity, improving datasets, and monitoring performance.

Once you understand overfitting and underfitting, you're ready to explore another fundamental question:

How do we actually measure whether a Machine Learning model is good?

Pixels to Perfection Design that Impresses

Want to partner with us? let's innovate together