AI Lesson 58 – GAN Basics (Generative Adversarial Nets) | Dataplexa

Overfitting and Underfitting

When training a machine learning model, the goal is not just to perform well on training data, but to perform well on new, unseen data. Two common problems prevent this from happening: overfitting and underfitting.

Understanding these concepts is critical because even a complex, high-accuracy model can completely fail in real-world use if it is not trained correctly.

Real-World Connection

Imagine preparing for an exam. If you memorize only past question papers word by word, you may score well on similar questions but fail when new questions appear. This is overfitting. If you barely study and only know basic definitions, you will perform poorly everywhere. This is underfitting.

What Is Underfitting?

Underfitting happens when a model is too simple to capture the patterns in the data. It fails to learn meaningful relationships, leading to poor performance on both training and test data.

  • Model is too simple
  • High bias
  • Poor training and test accuracy

What Is Overfitting?

Overfitting occurs when a model learns the training data too well, including noise and random fluctuations. While it performs very well on training data, it performs poorly on unseen data.

  • Model is too complex
  • High variance
  • Very high training accuracy, low test accuracy

The Bias–Variance Tradeoff

Bias represents error from overly simple assumptions. Variance represents error from sensitivity to small changes in training data. A good model balances both bias and variance.

Visual Intuition

A straight line trying to fit curved data underfits. A curve passing through every data point overfits. The best model captures the overall trend without memorizing noise.

Underfitting Example (Python)


from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
import numpy as np

X = np.array([[1], [2], [3], [4], [5]])
y = np.array([1, 4, 9, 16, 25])

model = LinearRegression()
model.fit(X, y)

predictions = model.predict(X)
print("MSE:", mean_squared_error(y, predictions))
  
MSE: 22.0

Understanding the Output

The linear model fails to capture the quadratic relationship, resulting in high error. This is a clear example of underfitting.

Overfitting Example (Conceptual)

A very deep decision tree may perfectly classify training data but fail on new data because it memorizes specific examples instead of learning general patterns.

How to Detect Overfitting and Underfitting

  • Compare training and validation performance
  • Plot learning curves
  • Use cross-validation

How to Fix Underfitting

  • Use a more complex model
  • Add more relevant features
  • Reduce regularization

How to Fix Overfitting

  • Simplify the model
  • Collect more training data
  • Apply regularization
  • Use feature selection

Practice Questions

Practice 1: What problem occurs when a model is too simple?



Practice 2: What problem occurs when a model memorizes training data?



Practice 3: What balance explains underfitting and overfitting?



Quick Quiz

Quiz 1: Overfitting is associated with which issue?





Quiz 2: A model performing poorly on both train and test data indicates?





Quiz 3: Which technique helps reduce overfitting?





Coming up next: Cross-Validation — testing models reliably using multiple data splits.