Overfitting and Underfitting
Complete the full lesson to earn 25 points
Work through each section, then tap “Mark as Complete” on the last one.
✦ Skip the page breaks and see fewer ads — read each lesson on a single page with Pro
Machine Learning Fundamentals: Overfitting and Underfitting
Introduction: The Goldilocks Problem of Machine Learning
When we build machine learning models, our primary objective is to create a system that performs well not just on the data we have already seen, but on data we have yet to encounter. This ability to perform accurately on unseen information is known as generalization. However, achieving perfect generalization is rarely straightforward. In the process of training a model, data scientists frequently encounter two primary failure modes: overfitting and underfitting. These two concepts represent the "Goldilocks" problem of machine learning—the challenge of finding the model that is "just right."
Overfitting occurs when a model learns the training data too well, effectively memorizing the noise and random fluctuations rather than the underlying patterns. Underfitting, conversely, happens when a model is too simple to capture the complexity of the data, failing to learn the relationship between the inputs and the target variable. Understanding these concepts is essential for anyone working within the Azure Machine Learning ecosystem, as the platform provides automated tools that attempt to balance these risks, but requires the practitioner to understand the underlying mechanics to make informed decisions.
In this lesson, we will explore the mathematical and conceptual foundations of these two phenomena. We will examine how they manifest in real-world datasets, how to diagnose them using evaluation metrics, and the specific strategies—such as regularization and hyperparameter tuning—that you can implement within your Azure-based projects to ensure your models are reliable and accurate.
Defining the Core Concepts
What is Underfitting?
Underfitting is a condition where a model has high bias. This means the model makes strong, incorrect assumptions about the data because it is not complex enough to represent the true underlying structure. For example, if you attempt to fit a straight line (linear regression) to data that follows a complex, parabolic curve, the model will consistently underperform. The model lacks the "capacity" to learn the nuances of the data. You will notice underfitting when the model performs poorly on both the training data and the validation data.
What is Overfitting?
Overfitting is a condition where a model has high variance. The model becomes so sensitive to the specific training examples that it learns the noise in the data as if it were a signal. If you have a dataset with some random errors or outliers, an overfitted model will adjust its internal parameters to account for those specific errors. While it might show a near-perfect accuracy score on the training set, it will struggle significantly when exposed to new, unseen data. This is a common issue in high-capacity models like deep neural networks or deep decision trees that are allowed to grow without constraints.
Callout: Bias-Variance Tradeoff The relationship between underfitting and overfitting is often described through the Bias-Variance Tradeoff. Bias refers to the error introduced by approximating a real-world problem with a simplified model. Variance refers to the error introduced by the model's sensitivity to small fluctuations in the training set. Reducing bias often increases variance, and reducing variance often increases bias. The goal of machine learning is to find the optimal point where the sum of these errors is minimized.
Diagnosing Models: The Role of Evaluation Metrics
To identify whether your model is suffering from underfitting or overfitting, you must look at the performance gap between your training set and your validation (or test) set. In the Azure Machine Learning studio, these metrics are automatically calculated during your experiments.
The Diagnostic Table
| Scenario | Training Performance | Validation Performance | Diagnosis |
|---|---|---|---|
| High Bias | Low Accuracy / High Error | Low Accuracy / High Error | Underfitting |
| High Variance | High Accuracy / Low Error | Low Accuracy / High Error | Overfitting |
| Optimal | High Accuracy / Low Error | High Accuracy / Low Error | Well-Balanced |
When you analyze your Azure ML run results, look for the "Training" and "Validation" curves. If the training loss curve continues to drop while the validation loss curve begins to rise, you have a classic case of overfitting. If both curves plateau at a high level of error, you are likely dealing with underfitting.
Practical Examples: A Regression Perspective
Imagine you are working on a project in Azure Machine Learning to predict house prices based on square footage.
The Underfitting Case
If you use a simple linear model: Price = Weight * SquareFootage + Bias. If the actual data has a non-linear relationship (e.g., price increases exponentially with size), your linear model will consistently predict lower prices for large houses and higher prices for small houses. The model is too "rigid" to follow the curve of the data.
The Overfitting Case
If you use a high-degree polynomial regression (e.g., a 20th-degree polynomial), the model will create a squiggly line that passes through every single data point in your training set. While the error on your training set might be zero, the model will make wild, nonsensical predictions for any house size that wasn't in your original data. It has "memorized" the house prices rather than learning the general rule of pricing.
Strategies for Mitigation
Once you have identified the problem, you need to apply the correct strategy. You cannot fix overfitting with more complexity, and you cannot fix underfitting with more data if the model structure is wrong.
How to Fix Underfitting
- Increase Model Complexity: Switch from a linear model to a more complex algorithm, such as a Random Forest or a Gradient Boosting machine, which can handle non-linear relationships.
- Feature Engineering: Create new features that better represent the underlying logic. For instance, if you are predicting temperature, adding the "time of day" as a feature might help a simple model perform better.
- Remove Regularization: If you have applied heavy regularization, it might be preventing the model from learning the patterns. Reduce the strength of the penalty terms.
- Train for Longer: In some cases, especially with iterative algorithms like neural networks, the model simply hasn't finished learning the patterns yet.
How to Fix Overfitting
- Gather More Data: The more examples a model sees, the harder it becomes to memorize noise. Large datasets act as a natural regulator.
- Apply Regularization: Use techniques like L1 (Lasso) or L2 (Ridge) regularization to penalize large coefficients. This forces the model to keep its parameters small and simple.
- Pruning/Early Stopping: For decision trees, restrict the maximum depth. In deep learning, stop the training process as soon as the validation error begins to increase.
- Feature Selection: Remove irrelevant or noisy features that may be confusing the model. If a feature has no predictive power, it only provides more opportunities for the model to "memorize" noise.
Implementation in Python (Scikit-Learn / Azure ML)
When working within an Azure Machine Learning notebook, you can observe these effects using standard libraries. Let's look at a code snippet that demonstrates how we might adjust a model to handle these issues.
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression, Ridge
from sklearn.pipeline import make_pipeline
from sklearn.metrics import mean_squared_error
# Create synthetic data
X, y = generate_complex_data()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# 1. Underfitting: Too simple (Linear)
model_underfit = LinearRegression()
model_underfit.fit(X_train, y_train)
# 2. Overfitting: Too complex (High-degree polynomial without regularization)
model_overfit = make_pipeline(PolynomialFeatures(degree=15), LinearRegression())
model_overfit.fit(X_train, y_train)
# 3. Balanced: Complex but Regularized
model_balanced = make_pipeline(PolynomialFeatures(degree=15), Ridge(alpha=1.0))
model_balanced.fit(X_train, y_train)
Explanation of the Code
In this example, the model_underfit is a basic linear regression. It lacks the capacity to capture the complexity of the generate_complex_data() function. The model_overfit uses a 15th-degree polynomial, which creates a very high-capacity model that will likely overfit. Finally, the model_balanced uses the same 15th-degree polynomial but adds a Ridge regression, which applies L2 regularization to keep the polynomial coefficients in check. This "regularized complexity" is often the sweet spot for machine learning models.
Note: The
alphaparameter inRidgeregression controls the strength of the regularization. A higher alpha means more regularization (which helps prevent overfitting), while an alpha of zero makes it equivalent to a standard linear regression.
Best Practices for Azure Machine Learning Users
When you are working within the Azure Machine Learning environment, you should leverage the platform's specific capabilities to manage the bias-variance tradeoff effectively.
1. Use Automated ML (AutoML)
Azure AutoML is designed to automatically try different algorithms and hyperparameter combinations. It essentially searches for the "Goldilocks" model for you. By setting the primary metric to something like normalized_root_mean_squared_error and enabling early stopping, you let the platform handle the heavy lifting of preventing overfitting.
2. Cross-Validation is Mandatory
Never rely on a single train-test split. Use k-fold cross-validation, which divides your data into "k" parts and trains the model "k" times, using a different part as the validation set each time. This provides a much more robust estimate of how your model will perform on new data and makes it much easier to detect if your model is overfitting to a specific subset of your training data.
3. Monitoring with Azure ML Experiments
Always look at the charts provided in the Azure ML Studio experiment runs. The platform provides real-time visualization of your metrics. If you see your training accuracy climbing steadily while your validation accuracy remains flat or declines, stop the experiment. This is a clear indicator that the model is entering an overfitting state.
4. Keep Data Pipelines Clean
Data leakage is a hidden source of overfitting. This happens when information from your test set accidentally "leaks" into your training set (for example, by calculating the mean of a column using the entire dataset before splitting it). Always perform your data transformations (scaling, normalization, imputation) inside your Azure ML pipeline, where the transformation parameters are learned only from the training split.
Common Pitfalls and How to Avoid Them
Pitfall 1: Ignoring the "Noise" in Data
Many practitioners assume that more data is always better. While this is generally true, if your data contains significant noise or incorrect labels, more data can actually make overfitting worse. Always perform data cleaning and outlier detection before feeding data into your model.
Pitfall 2: Over-tuning Hyperparameters
It is tempting to keep tweaking hyperparameters until you get the perfect score on your test set. However, if you tune your model based on the test set, you are effectively "training" the model on the test data. This leads to a model that performs well on the test set but fails in production.
- Solution: Use a dedicated "Validation" set for tuning and keep a "Holdout" test set that you only touch once at the very end of the project.
Pitfall 3: Using a Model that is Too Complex for the Data Size
If you have a small dataset, do not jump straight to deep learning or massive ensemble models. These models require large amounts of data to learn generalizable patterns. If your data is limited, a simpler model (like a decision tree or a linear model) will often outperform a complex one because it is less prone to overfitting.
Detailed Comparison: Model Complexity vs. Performance
To help you visualize how these concepts interact, let’s look at a comparison of different approaches to model selection.
| Model Type | Bias | Variance | Best Use Case |
|---|---|---|---|
| Linear Regression | High | Low | Simple, interpretable trends |
| Decision Trees (Deep) | Low | High | Complex, non-linear data |
| Random Forests | Medium | Medium | General-purpose, robust to noise |
| Deep Neural Networks | Very Low | Very High | Large datasets, unstructured data |
As you move down the table, the model becomes more complex. While the bias drops, the variance increases. You must balance this by using techniques like pruning (for trees) or dropout/regularization (for neural networks).
The Role of Feature Engineering in Overfitting
Feature engineering is often the most critical step in preventing both underfitting and overfitting. If you provide a model with too many redundant features, you increase the likelihood of the model finding "patterns" in the noise. Conversely, if you don't provide enough features, the model will underfit.
Feature Selection Techniques
- Correlation Analysis: Remove features that are highly correlated with each other. If two features provide the same information, you are essentially providing the model with redundant inputs that can lead to overfitting.
- Recursive Feature Elimination (RFE): This technique removes the least important features one by one until the model performance stabilizes. It is a great way to simplify a model while maintaining accuracy.
- Domain Knowledge: Always talk to the subject matter experts. They can tell you which variables are actually likely to drive the outcome and which are just coincidental noise.
Step-by-Step: Evaluating a Model in Azure ML
If you are following the standard Azure ML workflow, follow these steps to ensure you are effectively managing the bias-variance tradeoff:
- Data Split: Use the
Train-Test Splitmodule to ensure your validation set is representative of the whole. A common ratio is 80% training and 20% testing. - Baseline Model: Always start with a simple model (e.g., Logistic Regression or a shallow Decision Tree). This serves as your baseline. If your complex model doesn't beat this baseline, you are likely overcomplicating things.
- Cross-Validation: Use the
Cross-Validate Modelmodule. Look at the variance in the metrics across the different folds. If the performance varies wildly between folds, your model is not stable and is likely overfitting. - Hyperparameter Tuning: Use the
Tune Model Hyperparametersmodule. Focus on parameters that control complexity (e.g.,learning_rate,number_of_trees,max_depth). - Analyze Residuals: For regression, look at the residuals (the difference between predicted and actual values). If you see a pattern in the residuals, your model is underfitting—it is missing a systematic relationship that it should have captured.
The Danger of "Test Set Contamination"
One of the most dangerous mistakes a machine learning practitioner can make is "test set contamination." This happens when the test set is used to inform the training process in any way. For example, if you look at the performance of your model on the test set and then decide to change your hyperparameters to improve the test score, you have effectively trained the model on the test set.
When this happens, the test set is no longer an objective measure of how the model performs on unseen data. It is now part of the training data. The model will appear to be performing well, but it will fail when it is deployed to a production environment. To avoid this, always keep your test set completely isolated. Do not look at it, do not use it to tune your models, and do not use it for anything other than the final validation of your chosen model.
Summary Checklist for Model Success
To wrap up this lesson, here is a checklist you can use every time you build a model in Azure:
- Start Simple: Did you try a linear or simple baseline model first?
- Check the Curves: Does the training error drop while the validation error rises? (If yes, you are overfitting).
- Validate Robustly: Did you use k-fold cross-validation instead of a single split?
- Regularize: Have you applied L1/L2 regularization or limited the depth of your trees?
- Feature Selection: Have you removed redundant or noisy features?
- Generalization: Is your performance on the validation set close to your performance on the training set?
- Isolation: Is your test set completely untouched by the training and tuning process?
Key Takeaways
- Underfitting is a failure of capacity: Your model is too simple and lacks the complexity to understand the patterns in the data. You fix this by increasing model complexity or adding more relevant features.
- Overfitting is a failure of generalization: Your model has "memorized" the training data, including the noise. You fix this by collecting more data, simplifying the model, or using regularization.
- The Bias-Variance Tradeoff is central: You are always balancing the model's ability to represent the data (bias) against its sensitivity to the specific training examples (variance).
- Evaluation is the only way to know: Always compare training performance to validation performance. A large gap is a red flag for overfitting.
- Azure ML tools are your friends: Use Automated ML, built-in cross-validation, and experiment monitoring to automate the detection of these issues.
- Avoid data leakage at all costs: Ensure that your test data is never used during the training or feature selection process.
- Simpler is often better: If a simple model performs almost as well as a complex one, choose the simple one. It will be more robust, easier to maintain, and less likely to fail in production.
By internalizing these concepts, you shift from being a user of machine learning tools to being a practitioner who understands the "why" behind the results. Whether you are using Azure’s drag-and-drop interface or writing custom code in notebooks, these principles remain the foundation of building reliable, high-performing machine learning systems.
Enjoying the courses?
Everything stays free. Pro shows fewer ads, doubles your daily points limit so you progress twice as fast, and lets you read each lesson on one page.
- ✓ Fewer advertisements
- ✓ 2× daily points limit
- ✓ Distraction-free lessons