Learning and Validation Curves
Learning and Validation Curves in Scikit-Learn: A Beginner-Friendly Guide
Ever wondered how to know if your machine learning model is underfitting or overfitting?
That’s where learning curves and validation curves come in. They give you visual feedback on how well your model is learning — and whether it might need more data, less complexity, or better tuning.
In this post, we’ll break down what these curves are and how to generate them using Python and scikit-learn. We’ll use the built-in digits dataset, so you can run the code straight away.
🧪 Learning Curve
A learning curve shows how the model performance changes with the size of the training set. It helps answer questions like:
-
Should I get more training data?
-
Is my model underfitting?
-
Has my model already learned all it can?
🧪 Python Code: Learning Curve Example
🔧 Validation Curve
Using the cross-validation method, the validation curve function evaluates training and test performance over the range of hyperparameter values. The process results in two sets of scores that we can compare visually.
Using the cross-validation method, the validation curve function evaluates training and test performance over the range of hyperparameter values. The process results in two sets of scores that we can compare visually.
The function explores machine learning model performance over various values of specific hyperparameters.
A validation curve helps you understand how changing a hyperparameter (like the depth of a tree or number of neighbors) affects performance. It tells you:
-
Is my model too simple or too complex?
-
What’s the best value for this parameter?
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import validation_curve
from sklearn.svm import SVC
from sklearn.datasets import load_iris
data = load_iris()
X, y = data.data, data.target
param_range = np.logspace(-3, 3, 5)
train_scores, test_scores = validation_curve(
SVC(), X, y,
param_name="gamma",
param_range=param_range,
cv=5,
scoring="accuracy"
)
✅ Summary: When to Use These Curves
| Curve Type | Helps You Understand | Use When… |
|---|---|---|
| Learning Curve | Impact of training data size | You're deciding whether to gather more data |
| Validation Curve | Effect of model complexity/hyperparameters | You're tuning your model |