
Overfitting and Underfitting in ML
Hey there! If you're diving into machine learning, you've probably heard the terms overfitting and underfitting. These are two of the most common challenges you'll face when training models, and understanding them is crucial to building effective algorithms. Today, we're going to break down what they mean, why they happen, and how you can detect and prevent them.
What Are Overfitting and Underfitting?
Let's start with the basics. When you train a machine learning model, your goal is for it to learn patterns from the training data and then generalize well to new, unseen data. Overfitting occurs when your model learns the training data too well—including the noise and random fluctuations—so it performs excellently on the training set but poorly on new data. On the flip side, underfitting happens when your model is too simple to capture the underlying structure of the data, resulting in poor performance on both the training and test sets.
Imagine you're studying for an exam. If you memorize the answers to specific practice questions (overfitting), you might fail when faced with new questions. If you don't study enough (underfitting), you'll perform poorly on both familiar and new questions. A good model strikes a balance: it understands the concepts without memorizing the details.
Scenario | Training Performance | Test Performance | Problem |
---|---|---|---|
Overfitting | High | Low | Model too complex |
Underfitting | Low | Low | Model too simple |
Good Fit | High | High | Balanced model |
Why Do They Occur?
Several factors contribute to overfitting and underfitting. Let's explore the main culprits.
- Model complexity: If your model is too complex (e.g., a deep neural network with many layers), it might overfit. If it's too simple (e.g., linear regression for a nonlinear problem), it might underfit.
- Amount of training data: With insufficient data, even a simple model can overfit because it may start memorizing noise. More data generally helps the model generalize better.
- Noise in the data: Datasets with a lot of irrelevant information or errors can mislead the model, encouraging overfitting.
- Feature selection: Using too many features (especially irrelevant ones) can lead to overfitting, while using too few can cause underfitting.
Here's a simple code example in Python using scikit-learn to illustrate how model complexity affects fitting. We'll use a polynomial regression model.
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import Pipeline
from sklearn.model_selection import train_test_split
# Generate some sample data
np.random.seed(0)
X = np.linspace(0, 10, 100)
y = np.sin(X) + np.random.normal(0, 0.1, 100)
X = X.reshape(-1, 1)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
# Underfitting: linear model
model_linear = LinearRegression()
model_linear.fit(X_train, y_train)
train_score_linear = model_linear.score(X_train, y_train)
test_score_linear = model_linear.score(X_test, y_test)
# Overfitting: high degree polynomial
poly_features = PolynomialFeatures(degree=15)
model_poly = Pipeline([
('poly', poly_features),
('linear', LinearRegression())
])
model_poly.fit(X_train, y_train)
train_score_poly = model_poly.score(X_train, y_train)
test_score_poly = model_poly.score(X_test, y_test)
print(f"Linear model - Train score: {train_score_linear:.3f}, Test score: {test_score_linear:.3f}")
print(f"Polynomial model - Train score: {train_score_poly:.3f}, Test score: {test_score_poly:.3f}")
In this example, the linear model (underfitting) will have lower scores on both sets, while the high-degree polynomial (overfitting) will have a high training score but a lower test score.
How to Detect Overfitting and Underfitting
Detecting these issues is key to improving your model. Here are some practical ways to identify them.
- Use a validation set: Split your data into training, validation, and test sets. Monitor performance on the validation set during training.
- Learning curves: Plot training and validation accuracy/loss over time. If training accuracy is high but validation accuracy is low, you're likely overfitting. If both are low, it's underfitting.
- Cross-validation: Techniques like k-fold cross-validation give a more robust estimate of your model's performance on unseen data.
Let's generate learning curves for the previous example to visualize overfitting and underfitting.
from sklearn.model_selection import learning_curve
def plot_learning_curve(estimator, title, X, y, ylim=None, cv=None, n_jobs=-1):
plt.figure()
plt.title(title)
if ylim is not None:
plt.ylim(*ylim)
plt.xlabel("Training examples")
plt.ylabel("Score")
train_sizes, train_scores, test_scores = learning_curve(
estimator, X, y, cv=cv, n_jobs=n_jobs)
train_scores_mean = np.mean(train_scores, axis=1)
test_scores_mean = np.mean(test_scores, axis=1)
plt.grid()
plt.plot(train_sizes, train_scores_mean, 'o-', color="r", label="Training score")
plt.plot(train_sizes, test_scores_mean, 'o-', color="g", label="Cross-validation score")
plt.legend(loc="best")
return plt
# Plot for linear model (underfitting)
plot_learning_curve(LinearRegression(), "Learning Curves (Linear Regression)", X, y, cv=5)
plt.show()
# Plot for polynomial model (overfitting)
plot_learning_curve(model_poly, "Learning Curves (Polynomial Regression)", X, y, cv=5)
plt.show()
In the learning curves, for underfitting, both training and validation scores are low and close. For overfitting, the training score is high but the validation score is significantly lower.
Techniques to Prevent Overfitting
Now, let's talk about how to combat overfitting, which is often the trickier problem.
- Regularization: Add a penalty for large coefficients in the model. L1 (Lasso) and L2 (Ridge) regression are common techniques.
- Dropout: In neural networks, randomly ignore a subset of neurons during training to prevent co-adaptation.
- Early stopping: Stop training when the validation performance starts to degrade.
- Pruning: In decision trees, remove branches that have little importance.
- Data augmentation: Artificially increase the size of your training data by applying transformations (e.g., rotating images).
Here's an example of using Ridge regression (L2 regularization) to prevent overfitting.
from sklearn.linear_model import Ridge
# Without regularization
model_no_reg = LinearRegression()
model_no_reg.fit(X_train, y_train)
print(f"No regularization - Test score: {model_no_reg.score(X_test, y_test):.3f}")
# With regularization
model_ridge = Ridge(alpha=1.0) # alpha is the regularization strength
model_ridge.fit(X_train, y_train)
print(f"Ridge regression - Test score: {model_ridge.score(X_test, y_test):.3f}")
You'll often find that the regularized model performs better on the test set.
Techniques to Prevent Underfitting
Underfitting is usually easier to address: make your model more complex or provide more relevant features.
- Increase model complexity: Use a more powerful algorithm (e.g., switch from linear to polynomial regression).
- Feature engineering: Create new features that better capture the patterns in the data.
- Reduce regularization: If you're using regularization, try decreasing its strength.
- Train longer: For iterative models like neural networks, sometimes underfitting is due to insufficient training.
Let's see how increasing model complexity can help underfitting.
# Try a moderate polynomial degree to avoid underfitting without overfitting
poly_features_moderate = PolynomialFeatures(degree=3)
model_moderate = Pipeline([
('poly', poly_features_moderate),
('linear', LinearRegression())
])
model_moderate.fit(X_train, y_train)
print(f"Moderate polynomial - Train score: {model_moderate.score(X_train, y_train):.3f}, Test score: {model_moderate.score(X_test, y_test):.3f}")
This model should strike a better balance between bias and variance.
The Bias-Variance Tradeoff
Understanding overfitting and underfitting ties directly into the bias-variance tradeoff. Bias is the error due to overly simplistic assumptions, leading to underfitting. Variance is the error due to excessive complexity, leading to overfitting. Your goal is to minimize total error by finding the sweet spot between them.
Model Type | Bias | Variance | Total Error |
---|---|---|---|
Underfitting | High | Low | High |
Overfitting | Low | High | High |
Ideal Model | Low | Low | Low |
In practice, you'll need to experiment with different algorithms, hyperparameters, and data preprocessing steps to achieve this balance.
Practical Tips for Your Projects
Here are some actionable tips to help you avoid overfitting and underfitting in your machine learning projects.
- Always split your data into training, validation, and test sets.
- Start with a simple model and gradually increase complexity.
- Use cross-validation to get reliable performance estimates.
- Regularize your models appropriately.
- Monitor learning curves during training.
- Collect more data if possible—it's one of the best ways to improve generalization.
Remember, machine learning is an iterative process. Don't be discouraged if your first model doesn't perform well—tweaking and tuning is part of the journey!
I hope this article helps you better understand overfitting and underfitting. Happy modeling!