A machine learning model that scores 99% on the data it was trained on can still fail badly the moment it meets new data. On the other hand, a model can perform poorly everywhere because it never really learned anything useful. These two problems have names, overfitting and underfitting, and recognizing them is one of the most important skills in practical machine learning. This guide explains what each one means, how to detect them, and what you can do about them.
The real goal: generalization
When we train a model, we are not interested in how well it memorizes the training examples. We want it to generalize, meaning it should make good predictions on data it has never seen. A spam filter is only useful if it catches tomorrow’s spam, not just the emails it was trained on.
To measure generalization, data is usually split into separate sets:
- Training set: the examples the model learns from.
- Validation set: examples held back to compare models and tune settings.
- Test set: examples used only at the end, to estimate real-world performance honestly.
Comparing performance on the training set with performance on the validation set is the main way to diagnose overfitting and underfitting.
What is underfitting?
A model underfits when it is too simple to capture the patterns in the data. Picture trying to describe a curved relationship with a straight line: no matter how you place the line, it misses most of the points. An underfitting model performs poorly on the training data and on new data.
Common causes include:
- A model that is too simple for the problem (for example, a linear model for a strongly non-linear relationship).
- Missing or uninformative features, so the model simply does not have the information it needs.
- Too much regularization, which restricts the model so heavily that it cannot learn.
- Stopping training too early, especially with neural networks.
What is overfitting?
A model overfits when it learns the training data too well, including its noise and random quirks. Instead of learning the general rule, it memorizes specific examples. The classic sign is excellent training performance combined with noticeably worse validation performance.
Think of a student who memorizes the exact answers to last year’s exam instead of understanding the subject. They will ace a copy of that exam and struggle with any new question. Overfitting is the model equivalent.
Typical causes are:
- A model that is too complex relative to the amount of data (very deep decision trees, huge neural networks trained on small datasets).
- Too little training data, so random patterns look meaningful.
- Noisy data or labeling errors that the model treats as real signal.
- Training for too long without monitoring validation performance.
- Data leakage, where information from the validation or test set accidentally influences training, making results look better than they really are.
How to diagnose the problem
The quickest diagnosis comes from comparing training and validation errors:
| Training error | Validation error | Likely diagnosis |
|---|---|---|
| High | High | Underfitting |
| Low | Much higher than training | Overfitting |
| Low | Low and close to training | Good fit |
Learning curves make this even clearer. If you plot training and validation error as training progresses (or as you add more data), an overfitting model shows a widening gap: training error keeps falling while validation error flattens or starts rising. An underfitting model shows both curves stuck at a high error level, close to each other.
The bias–variance trade-off
These ideas connect to a classic concept called the bias–variance trade-off. Bias is error caused by overly simple assumptions; high bias leads to underfitting. Variance is error caused by excessive sensitivity to the particular training data; high variance leads to overfitting. Making a model more flexible usually lowers bias but raises variance, and vice versa. The goal is not to eliminate one of them but to find the balance that minimizes error on new data.
How to fix underfitting
- Use a more expressive model: move from linear to polynomial features, from a shallow tree to a deeper one, or to a larger network.
- Improve your features: add relevant variables or engineer new ones that capture the underlying relationship.
- Reduce regularization: loosen the constraints that are holding the model back.
- Train longer: give iterative models enough time to converge.
How to fix overfitting
- Get more data: more varied examples make it harder for the model to memorize noise. Data augmentation (for example, flipping or cropping images) can help when collecting new data is expensive.
- Simplify the model: limit tree depth, reduce the number of layers or parameters, or select fewer features.
- Apply regularization: techniques such as L1 and L2 penalties discourage overly large weights; dropout randomly disables neurons during neural network training.
- Use early stopping: monitor validation error and stop training when it stops improving.
- Use cross-validation: evaluating on several different splits gives a more reliable estimate and reduces the risk of tuning to one lucky split.
- Use ensembles: methods like random forests average many models, which tends to reduce variance.
- Check for leakage: make sure preprocessing steps such as scaling are fitted only on training data.
A simple example
Suppose you want to predict house prices from size. A straight line may miss the fact that prices rise faster for larger homes; that is underfitting. A very high-degree polynomial might pass through every training point exactly, twisting wildly between them and predicting absurd prices for sizes it has not seen; that is overfitting. A moderate curve that follows the general trend without chasing every point is usually the model that performs best on new houses.
Conclusion
Underfitting means the model has not learned enough; overfitting means it has learned the wrong things too well. By separating your data properly, comparing training and validation results, and reading learning curves, you can tell which problem you are facing and choose the right fix. Good machine learning is less about achieving a perfect training score and more about building models that hold up in the real world.
If you want to go deeper into model evaluation, neural networks and data science, explore the free Artificial Intelligence and Machine Learning courses available on Cursa and keep building your skills step by step.