Overfitting and Underfitting Explained
Understand overfitting and underfitting in machine learning, how to spot each one from training and validation results, and practical ways to fix them.
On this page
A machine learning model is useful only if it works on data it has never seen. Two failure modes stand in the way: overfitting and underfitting. They are opposite problems, and mixing them up leads to the wrong fix.
Think of a student preparing for an exam. One memorizes last year's answers word for word and panics when the questions change. Another barely studies and cannot answer anything. Models behave in the same two ways.
What underfitting looks like
An underfit model is too simple to capture the pattern in the data. It performs poorly on the training set and poorly on new data. Fitting a straight line to data that clearly follows a curve is the classic example.
Typical causes include:
- A model with too little capacity, such as a linear model for a non-linear problem.
- Features that do not carry enough information about the target.
- Training stopped too early, before the model had time to learn.
- Regularization set so strong that it forces the model to stay too plain.
In statistical terms, underfitting is a problem of high bias: the model makes strong, wrong assumptions about the shape of the data.
What overfitting looks like
An overfit model has learned the training data too closely, including its noise and quirks. It scores very well on the training set but noticeably worse on new data. It has memorized instead of generalized.
Imagine fitting a wiggly high-degree curve through every single point in a small, noisy dataset. The curve touches each point, yet it swings wildly between them and predicts nonsense for new inputs.
Common causes include:
- A model with far more capacity than the data justifies.
- A small training set.
- Noisy labels or irrelevant features.
- Training for too many rounds without any check.
This is a problem of high variance: the model's predictions change a lot depending on which specific examples it was trained on.
The bias and variance trade-off
These two problems sit on opposite ends of a trade-off. As you make a model more flexible, bias goes down because it can fit more patterns, but variance goes up because it can also fit noise. Making it simpler does the reverse.
The goal is not zero error on the training set. It is the sweet spot where error on unseen data is lowest. Total error can be thought of as bias, plus variance, plus irreducible noise that no model can remove.
How to diagnose which one you have
The simplest diagnostic is to compare performance on the training data with performance on a held-out validation set.
- Both scores are poor: the model is probably underfitting.
- Training score is strong, validation score is much weaker: the model is probably overfitting.
- Both scores are strong and close together: the model is generalizing well.
Learning curves make this clearer. Plot training and validation error as the amount of training data or the number of training rounds grows. Underfit models show two curves that stay high and close together. Overfit models show a wide gap, with training error low and validation error stuck higher.
Fixing underfitting
Because the model is too weak or too poorly informed, the cure is to give it more power or better input.
- Use a more flexible model, such as adding layers, trees or polynomial terms.
- Add or engineer more informative features.
- Train longer, and check that the learning rate is reasonable.
- Reduce regularization.
Adding more data usually does not help an underfit model by itself, because a model that is too simple cannot make use of extra examples.
Fixing overfitting
Here the model has too much freedom relative to the evidence, so the cure is to constrain it or give it more evidence.
- Collect more data, which is often the most effective remedy.
- Simplify the model, with fewer parameters, shallower trees or fewer layers.
- Apply regularization, such as L1 or L2 penalties that discourage large weights.
- Use dropout in neural networks, which randomly switches off units during training.
- Stop early, ending training when validation error starts to rise.
- Use data augmentation, creating varied copies of training examples such as flipped or cropped images.
- Use cross-validation to get a more reliable estimate of how the model will behave on new data.
A short code example
Here is a minimal way to see the effect with scikit-learn, comparing a very shallow decision tree to an unlimited one:
from sklearn.tree import DecisionTreeClassifier
shallow = DecisionTreeClassifier(max_depth=1)
deep = DecisionTreeClassifier(max_depth=None)
for model in (shallow, deep):
model.fit(X_train, y_train)
print(model.score(X_train, y_train), model.score(X_val, y_val))
The shallow tree will tend to score low on both sets, which signals underfitting. The unrestricted tree will tend to score near perfectly on training data but lower on validation data, which signals overfitting. A moderate depth usually lands between the two.
Keep the test set clean
Tuning a model against the validation set repeatedly can quietly overfit to that set too. That is why a final test set should stay untouched until you have chosen your model. It gives an honest estimate of real-world performance.
Frequently Asked Questions
Can a model be overfit and underfit at the same time?
Not in the same sense, but it can happen in different parts of the problem. A model may fit some regions of the data too loosely and other regions too tightly. Looking at errors across different subsets of the data can reveal this.
Is more data always the fix for overfitting?
It often helps a lot, but it is not guaranteed. If the extra data is low quality or from a different distribution, the problem may remain. Combining more data with regularization or a simpler model is usually more reliable.
How do I know when to stop training?
Watch the validation error during training. When it stops improving and begins to rise while training error keeps falling, the model has started to overfit. Early stopping halts training near that point.