Overfitting vs Underfitting in Machine Learning: Causes and Fixes
On this page
Short answer: Overfitting happens when a model learns the training data too closely, including its noise, so it scores very well on training data but poorly on new data. Underfitting happens when a model is too simple to capture the real pattern, so it performs poorly on both training and new data. You detect them by comparing training and validation scores: a large gap signals overfitting; low scores on both signal underfitting. The goal is a model in the middle that generalises well.
Key takeaways
- Overfitting = high training score, much lower validation score (high variance).
- Underfitting = low training and validation scores (high bias).
- Fix overfitting with simpler models, regularisation, more data or early stopping.
- Fix underfitting with more complex models, better features or less regularisation.
Overfitting vs underfitting at a glance
| Underfitting | Good fit | Overfitting | |
|---|---|---|---|
| Training score | Low | High | Very high |
| Validation score | Low | High (close to training) | Much lower than training |
| Error type | High bias | Balanced | High variance |
| Analogy | Student who didn’t study | Student who understood concepts | Student who memorised answers |
The bias-variance trade-off
- Bias: error from overly simple assumptions, which causes underfitting
- Variance: error from being too sensitive to the training data, which causes overfitting
Increasing model complexity usually lowers bias but raises variance. Good modelling finds the complexity where validation error is lowest.
How to detect them
Compare training and validation scores
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)
for depth in [1, 3, 5, 10, None]:
model = DecisionTreeClassifier(max_depth=depth, random_state=42)
model.fit(X_train, y_train)
print(depth, round(model.score(X_train, y_train), 3), round(model.score(X_val, y_val), 3))
Typically, a depth of 1 underfits (both scores low), while unlimited depth overfits (training near 1.0, validation lower).
Use cross-validation and learning curves
Cross-validation gives a more reliable validation estimate. Learning curves show training and validation scores as data size grows: a persistent gap suggests overfitting; two low, converged curves suggest underfitting. More on this in how to evaluate a machine learning model.
How to fix overfitting
- Get more training data (or use data augmentation for images and text)
- Simplify the model: lower tree depth, fewer features, fewer layers
- Regularisation: L1 (Lasso), L2 (Ridge), or a smaller
Cin logistic regression and SVM - Dropout and early stopping in neural networks
- Ensembles such as random forests, which average many trees
- Remove leakage: sometimes a “too good” score is data leakage, not overfitting
How to fix underfitting
- Use a more expressive model, for example gradient boosting instead of linear regression for non-linear data
- Add better features: interactions, aggregates, domain features. See Feature Engineering Explained
- Reduce regularisation
- Train longer (more epochs) for neural networks
Overfitting in deep learning and LLMs
Deep networks have millions of parameters and overfit easily on small datasets. Common defences are data augmentation, dropout, weight decay, early stopping and transfer learning from pre-trained models. When fine-tuning LLMs on small datasets, watch validation loss and use techniques such as LoRA with careful epochs. Read RAG vs fine-tuning.
Frequently asked questions
Is 100% training accuracy always overfitting?
Not always, but it’s a warning sign. Check validation performance and look for data leakage.
Can a model overfit and underfit at the same time?
Not in the usual sense, but a model can fit some segments of the data well and others poorly. Evaluate by segment.
Does more data always fix overfitting?
More representative data usually helps, but it won’t fix leakage or a fundamentally wrong model.
Is this a common interview question?
Yes. It’s one of the most frequently asked machine learning interview questions.
Conclusion
Overfitting vs underfitting comes down to how well a model generalises. Compare training and validation scores, use cross-validation and learning curves, and apply the right fix: regularise or simplify for overfitting; add complexity or features for underfitting.
Practise this on real datasets in our Machine Learning course.
Related reading
- Hyperparameter tuning: find the right model complexity
- How to evaluate a machine learning model
- Cross-validation: get a reliable performance estimate
Learn with Mehul Prajapati in Borivali West
At Mehul Prajapati’s institute, you get 100% practical, hands-on training by industry experts with a minimum of 5+ years of experience, plus job-oriented programmes with placement support. Weekday and weekend batches are available, with multiple 2-hour slots between 8 am and 6 pm.
Address: 12th Floor, 1214, Gold Crest Business Centre, LT Rd, Above Westside Showroom, Borivali West, Mumbai, Maharashtra 400092
Call: +91 99301 12627 / +91 96645 45072
Visit the centre or call us to choose the right course and batch and to know the current fee structure.