Overfitting and Underfitting

Why Model Performance Matters

The goal of machine learning is not simply to perform well on training data. A successful model should make accurate predictions on new, unseen data.

01

Learn patterns from training data

02

Generalize those patterns to new data

03

Make accurate predictions in the real world

Generalization

Generalization refers to a model's ability to perform well on data it has never seen before.

Example

Training Data — 1000 sq ft → $200,000, 1500 sq ft → $300,000

New House — 1800 sq ft

A well-generalized model can accurately predict the new house price even though it has never seen that exact house before.

What Is Underfitting?

Underfitting occurs when a model is too simple to learn important patterns in the data. The model performs poorly during training and testing because it never learns enough information.

Characteristics
  • High Bias
  • Low Training Accuracy
  • Low Testing Accuracy
  • Poor Predictions

Causes of Underfitting

Common Causes
  • Model too simple
  • Too few features
  • Insufficient training
  • Excessive regularization
  • Low model capacity

Signs of Underfitting

Indicators

Training Accuracy — Low

Testing Accuracy — Low

Low performance on both datasets is a strong indicator of underfitting.

What Is Overfitting?

Overfitting occurs when a model learns the training data too well. Instead of learning general patterns, it memorizes examples and noise found within the training dataset.

Characteristics
  • High Variance
  • Very High Training Accuracy
  • Lower Testing Accuracy
  • Poor Generalization

Causes of Overfitting

Common Causes
  • Model too complex
  • Too many features
  • Small dataset
  • Excessive training epochs
  • No regularization

Signs of Overfitting

Indicators

Training Accuracy — 99%

Testing Accuracy — 74%

A large gap between training and testing performance is one of the clearest signs of overfitting.

Training vs Testing Performance

Model TypeTraining AccuracyTesting Accuracy
UnderfittingLowLow
Good FitHighHigh
OverfittingVery HighLower

Bias and Variance

Bias measures how much a model oversimplifies a problem. Variance measures how sensitive a model is to training data.

High Bias

Model is too simple. Underfitting. Missed patterns and poor predictions.

High Variance

Model is too sensitive. Overfitting. Memorizes training data instead of generalizing.

Bias-Variance Tradeoff

One of the biggest challenges in machine learning is balancing bias and variance.

01

Too Much Bias — Model underfits

02

Too Much Variance — Model overfits

03

Balanced — Good generalization

Regularization

Regularization discourages overly complex models and helps reduce overfitting.

L1 Regularization (Lasso)

Encourages sparse models. Can eliminate unnecessary features entirely.

L2 Regularization (Ridge)

Reduces large model weights. Creates smoother and simpler models.

Cross-Validation

Cross-validation evaluates models using multiple training and validation splits.

5-Fold Cross Validation

Fold 1 → Validate, Folds 2-5 → Train

Fold 2 → Validate, Folds 1,3,4,5 → Train

Repeat until all folds are used.

Benefits — Better evaluation, more reliable estimates, helps detect overfitting

Early Stopping

Early stopping ends training before the model begins memorizing the training data.

01

Training continues while validation performance improves

02

Validation performance stops improving

03

Training stops and the best model is saved

Real-World Examples

Examples

Underfitting — Disease prediction using only age, ignoring blood pressure and medical history

Overfitting — Fraud model that memorizes specific transactions but fails on new fraud patterns

Good Generalization — Spam filter that learns characteristics of spam rather than memorizing specific emails

Overfitting vs Underfitting

CategoryUnderfittingOverfitting
Model ComplexityToo SimpleToo Complex
BiasHighLow
VarianceLowHigh
Training AccuracyLowVery High
Testing AccuracyLowLower

Practice Prompt

A fraud detection model achieves 99% training accuracy and 74% testing accuracy.

Determine whether the model is underfitting or overfitting. Explain why and suggest techniques that could improve performance.

Need Help?

Ask the AI if you need help understanding overfitting, underfitting, bias, variance, or any evaluation concept.