Choosing Between validation_curve and learning_curve for Model Diagnosis
validation_curve shows how one hyperparameter affects training and validation scores, while learning_curve shows how scores change with training set size. Use the first to tune the model and the second to decide whether collecting more data will help.
On this page
The short answer
Build a validation_curve when you suspect the model capacity or regularization is wrong and want to find a better hyperparameter value. Build a learning_curve when the model seems reasonable but you want to know whether more training samples would improve generalization. If the validation score on the learning curve still rises with sample size and a gap to the training score remains, collect more data; if both curves converge to a low plateau, change the model or features instead.
Two curves, two questions: what each one measures
A validation curve plots training and validation scores as a function of a single hyperparameter value, for example the regularization strength C of an SVC. It answers the question: for which value of this parameter does the estimator overfit or underfit? A learning curve plots the same two scores as a function of the number of training samples. It answers a different question: how much would the model gain from additional training data, and does it suffer more from bias or from variance? Keeping this framing in mind prevents the common mistake of tuning a hyperparameter when the real bottleneck is data volume, or vice versa.
Both functions come from sklearn.model_selection and return three arrays: the parameter values or train sizes, the per-fold training scores, and the per-fold validation scores. You typically average scores across folds before interpreting the shape of the curves.
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import validation_curve, learning_curve
from sklearn.svm import SVC
from sklearn.utils import shuffle
X, y = load_iris(return_X_y=True)
X, y = shuffle(X, y, random_state=0)
train_scores, valid_scores = validation_curve(
SVC(kernel="linear"), X, y,
param_name="C", param_range=np.logspace(-7, 3, 3),
)
print("validation train means:", train_scores.mean(axis=1).round(2))
print("validation valid means:", valid_scores.mean(axis=1).round(2))
sizes, tr, va = learning_curve(
SVC(kernel="linear"), X, y, train_sizes=[50, 80, 110], cv=5,
)
print("train sizes:", sizes)
print("learning train means:", tr.mean(axis=1).round(2))
print("learning valid means:", va.mean(axis=1).round(2))Practical tip
Wrap preprocessing and the estimator in a Pipeline before passing it to validation_curve or learning_curve, pass integers to CV splitters for comparable folds, and pass RandomState instances or None to randomized estimators so results are robust to initialization. Always confirm the tuned model on a held-out test set.
Diagnosing by signs: underfitting, overfitting, healthy
The scikit-learn documentation gives a simple reading rule. If the training score and the validation score are both low, the estimator is underfitting: it is too simple or too strongly regularized for the data. If the training score is high while the validation score is low, the estimator is overfitting: it memorizes the training folds but fails to generalize. Otherwise the model is working well. A low training score combined with a high validation score is usually not possible, so if you see it, suspect a bug in the pipeline, a scoring mistake, or a data problem rather than a genuine regime.
These signs apply to both curve types. On a validation curve you look for the parameter range where the healthy regime holds; on a learning curve you look at whether the gap between the curves narrows as samples grow.
When to change a hyperparameter: reading validation_curve
On a validation curve, the training score typically rises monotonically as model capacity grows (for example, as C increases and regularization weakens). The validation score usually rises first, then flattens or falls. The region where the validation score peaks, with a small gap to the training score, is where you should set the hyperparameter. In the documented iris example with SVC(kernel='linear'), the training scores rise from about 0.92 to about 0.98 as C moves across np.logspace(-7, 3, 3), while validation scores fluctuate around 0.9 to 1.0, letting you see where overfitting begins.
Remember that validation_curve handles one parameter at a time. If several hyperparameters interact, the proper tool is grid search or a similar method that optimizes the validation score over the joint space. Use the validation curve to understand and narrow the range for one parameter, then combine parameters in a search.
When to collect more data: reading learning_curve
The decision rule is straightforward. If the validation score keeps increasing with the number of training samples and the gap between training and validation scores is still narrowing at the largest size you can plot, adding more data will most likely improve generalization. This is the situation the documentation describes for an SVM on small data: the training score is much higher than the validation score, so more samples should help.
If instead both curves converge to a value that is quite low, as with the naive Bayes example in the documentation, more training data will probably not help much. In that case the model is limited by bias, not variance, and your effort should go into a more expressive model, better features, or a different algorithm rather than into data collection.
Connection to bias-variance and the limit of data collection
Generalization error decomposes into bias, variance, and noise. Bias is the average error across different training sets; variance is sensitivity to the training set; noise is a property of the data itself. Choosing more training data is one way to reduce variance, but the documentation is explicit: you should only collect more data if the true function is too complex to be approximated by an estimator with lower variance. If a simpler, lower-variance estimator can capture the true function, gathering samples is wasted effort.
In the one-dimensional polynomial example from the documentation, degree 1 underfits (high bias), degree 15 overfits (high variance), and degree 4 fits well. In one dimension you can see this by plotting; in high-dimensional spaces visualization is impossible, which is exactly why validation and learning curves exist as diagnostic tools.
Preventing leakage when building the curves
Every transformation used in training, such as StandardScaler, imputers, PCA, or feature selection, must be learned from training data only. If a normalization divides by the mean of all data, information from validation folds leaks into the model and scores become optimistically biased. The recommended practice is to split first, fit preprocessing on the train subset only, and transform both subsets with the fitted transformer.
The practical safeguard is a Pipeline: when you pass a pipeline to validation_curve or learning_curve, each cross-validation fold refits the transformers on that fold's training portion and applies transform to the validation portion, so the fit and transform methods are always applied to the correct subsets. This is why the pipeline is described as ideal for cross-validation and hyperparameter tuning functions.
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
pipe = make_pipeline(StandardScaler(), SVC(kernel="linear"))
# pass pipe instead of a bare estimator to validation_curve or learning_curveControlling randomness for reliable curves
Cross-validation splitters and some estimators are random objects. Passing an integer to a CV splitter makes every call to split produce the same folds, which is usually the safest option and enables fold-to-fold comparison across different estimators or search calls. Passing a RandomState instance to a splitter yields different splits on each call, which can inflate the apparent variance between two estimators compared on different splits.
For randomized estimators the recommendation is the opposite: pass a RandomState instance or leave random_state as None, so each fold's fit starts from a different RNG. This checks that the estimator is robust to its random initialization rather than lucky with one fixed seed; with an integer, the same RNG is reused on every fold and a good CV score might just reflect a fortunate seed. The documentation also advises against setting the global numpy seed with np.random.seed(0).
from sklearn.model_selection import KFold
from sklearn.ensemble import RandomForestClassifier
cv = KFold(shuffle=True, random_state=0) # integer: comparable folds
est = RandomForestClassifier(random_state=None) # instance/None: robust to initValidation score bias and the final check
Once you use a validation score to choose a hyperparameter, that score is biased and no longer a good estimate of generalization, because you effectively selected the value that performs best on those validation folds. To get a proper estimate, compute the score on a separate test set that played no role in tuning. Concretely: split off a test set before any curve work, use validation_curve and learning_curve on the remaining data, fix the hyperparameters, refit on the full training portion, and evaluate once on the test set.
Also keep the limits in mind. A validation curve shows one parameter at a time, so joint effects need a search. The conclusion that more data will help is an extrapolation from the curve's shape and does not guarantee a specific improvement. Finally, curves measure sample volume, not data quality or representativeness; a larger but skewed sample will not fix a sampling problem.
Things to check
- Confirm the validation score peaks in a parameter range with a small train-validation gap before fixing the hyperparameter.
- Confirm the learning curve validation score still rises at the largest plotted sample size before committing to data collection.
- Verify that preprocessing is inside a Pipeline so transformers are refit per fold without leakage.
- Verify CV splitters receive integer random_state values for fold-to-fold comparability.
- Verify randomized estimators use RandomState instances or None so results are robust to initialization.
- Hold out a test set untouched by tuning and report its score as the final generalization estimate.
Where this applies
validation_curve examines a single hyperparameter at a time and cannot reveal interactions; the learning-curve conclusion about more data is an extrapolation with no guaranteed gain; the tuned validation score is biased and requires a separate test set; and both curves measure sample volume, not data quality or representativeness.