Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 7

Visualizing Learning Curves for Model Diagnosis

Recommended resources

Hello! Welcome to the final lesson in our module on the Foundations of Supervised Learning and Evaluation.

In the last lesson, you did an excellent job implementing the ROC curve and AUC from scratch. These tools are fantastic for evaluating a final, trained model's performance across different thresholds. Today, we'll add another crucial diagnostic tool to your arsenal: learning curves.

While ROC curves assess the final product, learning curves help us diagnose how a model is learning and whether it's suffering from common ailments like underfitting or overfitting. Your goal for this lesson is to implement learning curve visualization to diagnose underfitting and overfitting. This skill is essential for deciding what to do next when your model isn't performing as you'd like—should you gather more data, use a more complex model, or add regularization? Learning curves will help you answer those questions.

1. Understanding Learning Curves

At its core, a learning curve is a plot that shows a model's performance as a function of the amount of training data. We typically plot two lines:

  1. Training Score: The model's performance on the data it was trained on.
  2. Validation Score: The model's performance on a separate, unseen set of data.

This allows us to see how well the model is learning (training score) and how well it's generalizing (validation score) as it gets more data.

To get a solid conceptual foundation, please watch the following video from a Cornell University course on Applied Machine Learning. It provides a clear and comprehensive explanation of learning curves.

Cornell CS 5787: Applied Machine Learning. Lecture 22. Part 1: Learning Curves

This video introduces learning curves, recaps the concepts of overfitting (high variance) and underfitting (high bias), and explains how to interpret the shapes of the training and validation curves.

Please watch from the beginning until 11:42. The video covers: The motivation for using learning curves (00:06 - 02:04). A review of overfitting (high variance) and underfitting (high bias) (02:04 - 04:25). A detailed breakdown of how to read and interpret the training and validation error curves as training set size increases (04:25 - 11:42).

Key Characteristics of Learning Curves

Based on the video, let's summarize the expected behavior:

  • Training Error/Score:

    • When the training set is very small, the model can easily fit it perfectly, so the error is low (or score is high).
    • As the training set size increases, it becomes harder for the model to fit all the points perfectly, so the training error increases (or score decreases) and eventually plateaus.
  • Validation Error/Score:

    • When the training set is very small, the model is poorly trained and can't generalize well, leading to high validation error (or a low score).
    • As the training set size increases, the model learns more robust patterns, so the validation error decreases (or score increases) and eventually plateaus.
Learning Curve Schematic
A typical learning curve plots model performance (score) against training set size. The training score (blue) decreases as the model tries to fit more data, while the validation score (red) increases as the model generalizes better. The gap between them indicates variance, and where they converge indicates the model's overall performance.

2. Diagnosing Problems with Learning Curves

The real power of learning curves comes from interpreting the patterns they reveal about our model. The shapes of the curves and the gap between them are clear indicators of bias and variance.

The next segment of the same Cornell video explains exactly how to diagnose these issues.

Cornell CS 5787: Applied Machine Learning. Lecture 22. Part 1: Learning Curves

This part of the video focuses on the specific learning curve patterns that indicate high bias (underfitting) and high variance (overfitting).

Now, watch from 11:42 to 16:24. Pay close attention to the visual signatures of each problem.

Here's a summary of the diagnostic patterns:

Learning Curves for Underfitting and Overfitting
This image contrasts the learning curves for underfitting (high bias) and overfitting (high variance) models. On the left, both training and validation errors converge to a high value, indicating underfitting. On the right, a large gap persists between the low training error and the high validation error, indicating overfitting.
  • High Bias (Underfitting):

    • Signature: Both the training and validation errors converge to a similarly high value.
    • Interpretation: The model is too simple to capture the underlying structure of the data. It performs poorly on both the training set and the validation set.
    • What to do: Adding more training data will not help. The curves have already plateaued. You need to use a more complex model (e.g., add polynomial features, use a more powerful algorithm) or add more informative features to the data.
  • High Variance (Overfitting):

    • Signature: There is a large and persistent gap between the training and validation errors. The training error is low, while the validation error is much higher.
    • Interpretation: The model is too complex and is memorizing the training data, including its noise, instead of learning the general pattern. It fails to generalize to new data.
    • What to do: Adding more training data is likely to help. The validation curve is still decreasing, and more data will help the model learn a more generalizable pattern, shrinking the gap. Other solutions include reducing model complexity or applying regularization (which we'll cover later).

3. Implementing Learning Curves in Python

As you prefer, let's focus on the implementation. The general algorithm to generate learning curve data is:

  1. Choose several different training set sizes.
  2. For each size:
    a. Take a random subset of the training data of that size.
    b. Train your model on this subset.
    c. Evaluate the model on both the training subset and the full validation set.
    d. To make the results more robust, repeat this process several times (like in k-fold cross-validation) and average the scores.
  3. Plot the averaged scores against the training set sizes.

Fortunately, scikit-learn has a built-in function, learning_curve, that handles this entire cross-validated procedure for us. It's an excellent tool that automates the logic we just described.

The following resource provides a great walkthrough of how to use this function and interpret its results, which we will use as a guide for our implementation.

Learning Curves and Validation Curves in Scikit-Learn

The article 'Learning Curves and Validation Curves in Scikit-Learn' demonstrates how to use scikit-learn's learning_curve function and provides clear examples of underfitting and overfitting.

Read the sections 'Learning Curves For Diagnosing Bias and Variance' and 'Learning Curves in scikit-learn'. Focus on the explanation of what a learning curve is and how the learning_curve function works, including its parameters (estimator, X, y, train_sizes, scoring) and what it returns.

Now, let's write a reusable Python function to generate and plot learning curves for any given model. We'll test it on synthetic data where we can control the complexity and clearly see the effects of underfitting and overfitting.

import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import learning_curve
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline

def plot_learning_curve(estimator, title, X, y, ylim=None, cv=None, n_jobs=None, train_sizes=np.linspace(.1, 1.0, 5)):
    """
    Generate a simple plot of the test and training learning curve.
    """
    plt.figure(figsize=(10, 6))
    plt.title(title)
    if ylim is not None:
        plt.ylim(*ylim)
    plt.xlabel("Training examples")
    plt.ylabel("Score (R^2)")

    train_sizes, train_scores, test_scores = learning_curve(
        estimator, X, y, cv=cv, n_jobs=n_jobs, train_sizes=train_sizes, scoring='r2')
    
    train_scores_mean = np.mean(train_scores, axis=1)
    train_scores_std = np.std(train_scores, axis=1)
    test_scores_mean = np.mean(test_scores, axis=1)
    test_scores_std = np.std(test_scores, axis=1)

    plt.grid()

    plt.fill_between(train_sizes, train_scores_mean - train_scores_std,
                     train_scores_mean + train_scores_std, alpha=0.1, color="r")
    plt.fill_between(train_sizes, test_scores_mean - test_scores_std,
                     test_scores_mean + test_scores_std, alpha=0.1, color="g")
    
    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

# --- Generate Synthetic Data ---
# A non-linear relationship that a simple linear model will fail to capture.
np.random.seed(0)
n_samples = 100
X = np.linspace(0, 10, n_samples).reshape(-1, 1)
y = np.sin(X).ravel() + np.random.randn(n_samples) * 0.5

# --- Case 1: Underfitting (High Bias) ---
# A simple linear model trying to fit a sine wave.
title = "Learning Curve (High Bias - Linear Regression)"
estimator_bias = LinearRegression()
plot_learning_curve(estimator_bias, title, X, y, ylim=(0.0, 1.01), cv=5)
plt.show()

# --- Case 2: Overfitting (High Variance) ---
# A very complex polynomial model (degree 15)
title = "Learning Curve (High Variance - Polynomial Degree 15)"
estimator_variance = make_pipeline(PolynomialFeatures(degree=15), LinearRegression())
plot_learning_curve(estimator_variance, title, X, y, ylim=(0.0, 1.01), cv=5)
plt.show()

# --- Case 3: Good Fit ---
# A polynomial model with a reasonable degree (4)
title = "Learning Curve (Good Fit - Polynomial Degree 4)"
estimator_good = make_pipeline(PolynomialFeatures(degree=4), LinearRegression())
plot_learning_curve(estimator_good, title, X, y, ylim=(0.0, 1.01), cv=5)
plt.show()

Interpreting the Plots:

  • High Bias Plot: Notice how both the training and cross-validation scores converge to a low value. The model is too simple. Even with all the data, it can't achieve a good score. This is classic underfitting.
  • High Variance Plot: Look at the huge gap between the red (training) and green (validation) curves. The training score is nearly perfect (1.0), but the validation score is much lower. The model has memorized the training data but fails to generalize. This is classic overfitting.
  • Good Fit Plot: Here, both scores converge to a high value, and the gap between them is small. The training score is slightly higher, which is normal, but the model performs well on both seen and unseen data. This is what you aim for.
Test your understanding!

You've trained a model and its learning curve shows that the validation score plateaus at 0.60, while the training score plateaus at 0.95.

  1. What problem is your model most likely suffering from?
  2. Your colleague suggests spending the next month collecting twice as much data. Based on the learning curve, is this a good use of time? Why or why not?
Show answer
  1. The model is suffering from high variance (overfitting). The large gap between the high training score (0.95) and the much lower validation score (0.60) is the key indicator.
  2. This is likely a bad use of time. The prompt states that the validation score has already plateaued. This means that adding more data is unlikely to significantly improve the model's performance on unseen data. While adding data is a standard fix for overfitting, if the curve has flattened, it signals that you've reached the limit of what can be gained with more data for this specific model configuration. A better use of time would be to try reducing the model's complexity or adding regularization.

Conclusion

Congratulations on completing the first module! You've built a robust toolkit for model evaluation and diagnostics from scratch.

Key Takeaways from this Lesson:

  • Learning curves plot model performance against training set size for both training and validation sets.
  • They are a primary tool for diagnosing bias (underfitting) and variance (overfitting).
  • High bias is identified when training and validation scores converge at a low level. The solution is to increase model complexity, not add more data.
  • High variance is identified by a large gap between a high training score and a low validation score. The solution is often to add more data (if the curve hasn't plateaued), reduce model complexity, or use regularization.
  • The sklearn.model_selection.learning_curve function is a practical and robust tool for generating this data using cross-validation.

Preview of the Next Module:
You now have a solid foundation for evaluating any supervised learning model you build. In the next module, we will shift gears from evaluation to construction. We'll start with the engine that drives most of machine learning: optimization. The next lesson will introduce the core concepts of optimization by explaining the role of a loss function and a model's parameters. This will set the stage for you to implement the famous gradient descent algorithm.