Generalization - Overfitting Vs Underfitting - Bias Vs Variance
π§ 1. What is Generalization?
πΉ Definition
Generalization is the ability of a machine learning model to perform well on unseen data, not just the data it was trained on.
Formally:
A model generalizes well if its prediction error on new data is low.
π Training vs Generalization Error
- Training error → error on training data
- Generalization error (test error) → error on unseen data
π A good model:
- Low training error ✅
- Low generalization error ✅
π― Core Objective of Machine Learning
We are NOT trying to minimize training error alone.
We want to minimize:
This is often called:
- True risk
- Expected loss
⚠️ 2. What is Overfitting?
πΉ Definition
Overfitting occurs when a model learns:
- the underlying pattern ✅
- AND noise/random fluctuations ❌
π Intuition
Imagine fitting a curve:
- Simple model → misses pattern → underfitting
- Moderate model → captures pattern → good generalization
- Very complex model → fits every point → overfitting
π Key Symptom
| Metric | Behavior |
|---|---|
| Training error | Very low |
| Validation/Test error | High |
π Conceptual Visualization
- Training error ↓ continuously
-
Validation error:
- ↓ initially
- then ↑ (after a point)
π That turning point = start of overfitting
π§©Why Overfitting Happens
πΉ 1. Model too complex
- Too many parameters
- High flexibility
Examples:
- Deep neural networks
- High-degree polynomial regression
πΉ 2. Small dataset
- Not enough data to represent true distribution
πΉ 3. Noise in data
- Model learns random variations
πΉ 4. Too much training
- Especially in neural networks
πΉ 5. Data leakage
- Model accidentally sees test information
π§ͺDetecting Overfitting
π Using validation set:
- Training accuracy ↑
- Validation accuracy ↓
π Learning curves:
Plot:
- Training error
- Validation error
Interpretation:
- Large gap → overfitting
- Both high → underfitting
π§ 3. What is Underfitting?
πΉ Definition
Underfitting occurs when a model is too simple to capture the underlying structure of the data.
The model fails to learn even the basic patterns.
π Key Characteristics
| Metric | Behavior |
|---|---|
| Training error | High ❌ |
| Validation/Test error | High ❌ |
π Unlike overfitting, the model performs poorly everywhere.
π Intuition
Imagine trying to fit a straight line to data that clearly follows a curve:
- The model is too rigid
- It cannot adapt to the pattern
- Result → large errors
π Conceptual Visualization
Think in terms of model complexity:
| Model Complexity | Behavior |
|---|---|
| Very low | Underfitting |
| Moderate | Good generalization |
| Very high | Overfitting |
π§© Causes of Underfitting
πΉ 1. Model Too Simple
Examples:
- Linear model for nonlinear data
- Shallow decision tree
πΉ 2. Insufficient Features
- Important variables missing
- Poor feature representation
πΉ 3. Too Much Regularization
- Model is overly constrained
πΉ 4. Inadequate Training
- Not trained long enough
- Optimization not converged
πΉ 5. Poor Data Representation
- Lack of feature engineering
- Raw data not informative enough
π§ͺ Detecting Underfitting
π Learning Curves
- Training error → high
- Validation error → high
- Gap between them → small
π Key insight:
Model cannot even fit training data
π Performance Indicators
- Low accuracy on training data
- High loss values
- Predictions too simplistic
π ️ How to Fix Underfitting
πΉ 1. Increase Model Complexity
-
Use more powerful models:
- Polynomial regression instead of linear
- Deeper neural networks
πΉ 2. Add Better Features
- Feature engineering
- Domain knowledge
πΉ 3. Reduce Regularization
- Lower penalty strength
πΉ 4. Train Longer
- Increase epochs (for neural networks)
πΉ 5. Use Nonlinear Models
- Kernel methods
- Tree-based models
π§ Intuitive Analogy (Teaching Friendly)
Student analogy:
- Underfitting = student didn’t study enough
- Cannot answer even basic questions
⚖️Bias–Variance Tradeoff
This is the theoretical backbone of generalization.
πΉ Bias
- Error due to overly simple assumptions
- High bias → underfitting
πΉ Variance
- Error due to sensitivity to training data
- High variance → overfitting
π Tradeoff
| Model Type | Bias | Variance |
|---|---|---|
| Simple | High | Low |
| Complex | Low | High |
π― Goal:
Find a balance:
Low bias + Low variance = Good generalization
πMathematical View
The terms bias and variance come from statistics, and the names describe what type of error the model is making.
Bias: Why is it called "bias"?
Bias means a systematic tendency toward a particular direction. A model with high bias consistently makes similar mistakes because of strong assumptions.
Mathematically:
where:
- = true function
- = predicted function
- = average prediction over many training datasets
If the average prediction is consistently away from the true value, the model is biased.
Example
Suppose actual relationship:
but we force a linear model:
No matter how many datasets we use, the model keeps trying to fit a straight line to a curve.
The error occurs because the model is biased toward linearity.
Hence it is called bias.
Think of a dartboard:
- All arrows land together but away from the center.
- Predictions are consistently shifted in one direction.
Center (true value)
X X X
X X X ← tightly grouped
O ← true value
The model repeatedly misses the target in the same way.
Variance: Why is it called "variance"?
Variance means how much something changes or spreads out.
Mathematically:
It measures how much predictions vary if we train the model on different datasets.
Example
Suppose we collect slightly different training samples:
Dataset 1 → model predicts 20
Dataset 2 → model predicts 35
Dataset 3 → model predicts 12
Dataset 4 → model predicts 40
Predictions change drastically.
The model is sensitive to small changes in data.
Hence it is called variance because predictions have large variability.
Dartboard analogy:
X X
O
X X
Predictions are scattered around.
Why both terms together?
The total prediction error can be decomposed as:
High bias:
- Model too simple
- Underfitting
- Consistent errors
High variance:
- Model too complex
- Overfitting
- Sensitive to training data
A simple way to remember:
- Bias = wrong assumptions → consistently wrong
- Variance = sensitivity to data → unstable predictions
The names come directly from their statistical meanings: bias = systematic deviation, variance = spread/variability.
π¬ Generalization in Practice
πΉ Empirical Risk Minimization (ERM)
Model minimizes:
BUT we actually care about:
π Overfitting happens when:
π ️Techniques to Improve Generalization
πΉ 1. More Data
- Best solution (if possible)
πΉ 2. Regularization
Adds penalty to model complexity:
Examples:
- L1 regularization (Lasso)
- L2 regularization (Ridge)
πΉ 3. Early Stopping
- Stop training when validation error increases
πΉ 4. Cross-Validation
- More reliable performance estimate
πΉ 5. Simpler Model
- Reduce parameters
πΉ 6. Dropout (Neural Networks)
- Randomly disable neurons during training
πΉ 7. Data Augmentation
- Create more training examples
π§ Intuitive Analogy (Good for Teaching)
Student analogy:
- Generalization → ability to solve new questions
- Overfitting → memorizing past exam answers
π A student who memorizes:
- Scores high on practice questions
- Fails new questions
π Model Complexity vs Performance
| Complexity | Training Error | Test Error |
|---|---|---|
| Low | High | High |
| Medium | Low | Lowest |
| High | Very Low | High |
π Relation to Training / Validation / Test Sets
- Training set → learning
- Validation set → detect overfitting
- Test set → measure generalization
πUnderfitting vs Overfitting
| Aspect | Underfitting | Overfitting |
|---|---|---|
| Model complexity | Too low | Too high |
| Training error | High | Very low |
| Test error | High | High |
| Generalization | Poor | Poor |
π― Relationship to Generalization
- Underfitting → poor generalization because model is too simple
- Overfitting → poor generalization because model is too complex
π Both are undesirable
π Goal: balanced model
π― Key Takeaways
- Underfitting = model too simple
- Cannot capture patterns
- High error everywhere
- Fixed by increasing model capacity or improving features
- Low training error ≠ good model
- Generalization is the real goal
- Overfitting = learning noise
- Test error will be high is the model overfits
- Validation set is essential
- Balance model complexity carefully



Comments
Post a Comment