Understanding Overfitting and Underfitting in Predictive Modeling

In this lesson, we will deepen our understanding about two common challenges faced while training machine learning models: overfitting and underfitting.

First, let's define these terms. Overfitting happens when a model learns the training data so well that it even catches the irrelevant details or noise in the data. Thus, it performs well on the training data but fails on unseen data or test data because it's unable to generalize the patterns for new, real-world data.

On the contrary, underfitting happens when a model performs poorly on both the training and test data because it cannot capture the underlying pattern of the data. This situation is mostly due to the simplicity of the model, and it too fails to generalize on the unseen data. In terms of error rates, overfitting gives a low training error but a high test error, while underfitting gives a high error for both datasets.

Avoiding Overfitting and Underfitting

Finding the balance between overfitting and underfitting while training models is crucial. Here are some techniques to avoid overfitting or underfitting:

  • Regularization: Regularization techniques add a penalty term to the error of the model to limit the permissible complexity of the model. They are effective for overfitting prevention by making the model simpler and more general.

  • Adding More Data: A larger training dataset may help decrease overfitting, because the more data we have, the better our model can learn from it and generalize upon unseen data.

  • Early Stopping: Early stopping is a technique to avoid overfitting by halting the training once the model's performance begins to degrade on a held-out validation set.

  • Cross-Validation: Cross-Validation splits the dataset into multiple parts, training on some and validating on others to assess model consistency and detect overfitting or underfitting by evaluating performance across varied data subsets.

The key is to reach a fair trade-off between bias (underfitting) and variance (overfitting) such that your model works well on unseen data.

Building Overfitted and Underfitted SVM Regressors

Now let's dive into practical demonstration using a toy dataset and Support Vector Machines (SVM) for regression tasks. Our focus here is to illustrate the concepts of overfitting and underfitting by tweaking the parameters of SVM regressors.

In the context of SVM (Support Vector Machine) regression, the parameter 'C' plays a pivotal role. It is a regularization parameter that directly influences the model's complexity and its ability to adapt to the data. Specifically, 'C' balances how much the SVM should prioritize fitting each individual data point. When 'C' is high, the model endeavors to fit the training data as closely as possible, potentially leading to overfitting by emphasizing minor fluctuations and noise in the data. On the other hand, a low 'C' makes the model more tolerant to errors on individual data points, leading to a simpler, more generalizing model but at the risk of underfitting if it becomes too simplistic to capture the underlying trend. Thus, 'C' acts as a lever for adjusting the trade-off between capturing the data's nuances (low bias, high variance) and maintaining generalizability for unseen data (high bias, low variance).

  • A high 'C' value leads to overfitting by allowing the algorithm to penalize the error term more aggressively. This results in the model trying hard to fit all the training data points, even capturing the noise in the data, which usually hurts its ability to generalize on unseen datasets.
  • Conversely, a low 'C' value contributes to underfitting by easing the penalty on the error term, which makes the model too simplistic. This simplicity might prevent the model from capturing the essence and complexities of the dataset, performing poorly on both the training and unseen data.

Let's explore this behavior with hands-on experimentation:

# Import necessary libraries
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from sklearn.svm import SVR
from sklearn.metrics import mean_squared_error
from math import sqrt

# Generate a toy dataset
X, y = make_regression(n_samples=1000, n_features=20, noise=0.1, random_state=42)

# Split the data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Create and fit an overfitted SVM regressor with high C
overfitted_model = SVR(kernel='rbf', C=1000.0)
overfitted_model.fit(X_train, y_train)

# Create and fit an underfitted SVM regressor with low C
underfitted_model = SVR(kernel='rbf', C=0.001)
underfitted_model.fit(X_train, y_train)

With our models built, we're prepared to move on to the evaluation phase, which will allow us to assess the implications of overfitting and underfitting in a tangible, measurable way.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal