When evaluating a machine learning model, simply looking at a single metric like accuracy or F1-score doesn't always tell the full story. Learning curves are powerful diagnostic tools that help you understand your model's performance by plotting its training error and validation error against the amount of training data used. By visualizing how these errors change as your model sees more data, you can diagnose fundamental issues like high bias (underfitting) or high variance (overfitting), guiding your next steps in model improvement.
Interpreting learning curves is key. If both the training and validation error are high and converge at a relatively high error rate, your model likely suffers from high bias (underfitting). This means the model is too simple for the data, and even adding more data won't help much; you'd need a more complex model or better features. Conversely, if the training error is very low but the validation error is significantly higher, with a large gap between the curves, your model is experiencing high variance (overfitting). In this scenario, the model has memorized the training data too well; adding more data, applying regularization, or simplifying the model could be beneficial.
Beyond just classifying instances correctly, sometimes you need to trust your model's predicted probabilities. This is where calibration plots, also known as reliability diagrams, come in. A calibration plot assesses how well your model's predicted probabilities match the actual proportion of positive outcomes for different probability bins. For instance, if your model predicts a 70% chance of an event happening for 100 samples, a perfectly calibrated model would see that event truly happen in approximately 70 of those samples. A perfectly calibrated model's predictions would lie along the diagonal line on the plot. Models like Logistic Regression tend to be well-calibrated by default, while others, such as Naive Bayes or uncalibrated Support Vector Machines, often require post-hoc calibration techniques (e.g., Isotonic Regression, Platt Scaling) to make their probability outputs more trustworthy for critical decision-making where the "likelihood" itself is as important as the final prediction.
Key Takeaways
- Learning curves diagnose bias (underfitting) and variance (overfitting) by plotting error against the amount of training data.
- High bias manifests as both training and validation errors converging at a high level; high variance shows a large gap between low training error and high validation error.
- Calibration plots assess the reliability of a model's predicted probabilities, comparing them to actual observed frequencies.
- A perfectly calibrated model's probability predictions align with the true proportion of positive outcomes, visualized as a diagonal line on the plot.
- Use learning curves to inform decisions on model complexity or data quantity; use calibration plots when the trustworthiness of probability estimates is crucial (e.g., risk assessment).
Code Example
import numpy as np
import matplotlib.pyplot as plt
from sklearn.naive_bayes import GaussianNB
from sklearn.model_selection import learning_curve
from sklearn.datasets import load_digits
# Load a sample dataset
X, y = load_digits(return_X_y=True)
# Create a classifier
clf = GaussianNB()
# Generate learning curves
train_sizes, train_scores, test_scores = learning_curve(
clf, X, y, cv=5, n_jobs=-1,
train_sizes=np.linspace(0.1, 1.0, 10))
train_scores_mean = np.mean(train_scores, axis=1)
test_scores_mean = np.mean(test_scores, axis=1)
plt.plot(train_sizes, train_scores_mean, 'o-', label="Training score")
plt.plot(train_sizes, test_scores_mean, 'o-', label="Cross-validation score")
plt.title("Learning Curve")
plt.xlabel("Training examples")
plt.ylabel("Score")
plt.legend(loc="best")
plt.grid()
plt.show()How this code works
This code generates a learning curve plot, a vital tool for understanding how a model's performance changes with varying amounts of training data. It helps diagnose if a model is underfitting (potentially needs more complexity or features) or overfitting (might benefit from more data or regularization).
First, essential libraries like numpy for calculations, matplotlib.pyplot for plotting, and sklearn components are imported. A sample dataset, load_digits, is loaded, and a GaussianNB classifier is initialized. The core logic resides in sklearn.model_selection.learning_curve. This function repeatedly trains and evaluates the clf model using different subsets of the training data, specified by train_sizes (ranging from 10% to 100% of the data in 10 steps using np.linspace). Crucially, for each training subset size, learning_curve performs 5-fold cross-validation (cv=5). This means it internally splits that subset five times into training and validation folds, producing multiple train_scores and test_scores. These scores are then averaged using np.mean(..., axis=1) to get a single performance value for each training size.
Finally, matplotlib.pyplot is used to plot these averaged training and cross-validation scores against the train_sizes. The plot includes labels, a title, a legend, and a grid for clarity before plt.show() displays it. A subtle but important detail is how learning_curve handles the evaluation: it doesn't just apply cv to the full dataset. Instead, it creates smaller subsets of the training data first, and then applies cross-validation within each of those subsets. This comprehensive internal process ensures a robust assessment of performance across different data volumes, which is key to understanding the model's behavior.