Sub topic 5
Choosing a scoring metric
This chapter assumes you know how to fit a scikit-learn estimator, what cross-validation does, and the difference between training and validation scores. Both learning_curve and validation_curve take a scoring argument. That argument decides what the y-axis means, so it decides what the curves can tell you.
Why the metric shapes the curve
A curve plots one number per training size or hyperparameter value. If that number is a poor summary of what you care about, the diagnosis will be poor too. A model can look fine on accuracy and fail on the cases that matter.
Consider a dataset where 95% of samples belong to one class. A model that always predicts that class scores 0.95 accuracy. Its learning curve would look flat and high, and you would conclude the model is doing well. Switching to a metric that is sensitive to the minority class shows the real picture.
The scoring argument
Both functions accept a string name or a scorer object. All scorers follow one convention: higher is better. Metrics that measure error, such as mean squared error, are therefore negated.
| Task | Common `scoring` strings | Use when |
|---|---|---|
| Classification, balanced classes | `"accuracy"` | Classes are roughly equal in size |
| Classification, imbalanced classes | `"balanced_accuracy"`, `"f1"`, `"average_precision"` | The minority class matters most |
| Classification, ranking quality | `"roc_auc"` | You care about scores, not a fixed threshold |
| Multiclass averaging | `"f1_macro"`, `"f1_weighted"` | You need one number across many classes |
| Regression | `"r2"`, `"neg_mean_squared_error"`, `"neg_mean_absolute_error"` | You are predicting a continuous value |
If you omit scoring, scikit-learn uses the estimator's own score method. That is accuracy for classifiers and R² for regressors. Naming the metric explicitly makes your code clearer and your results easier to reproduce.
Computing a curve with a different metric
The next example builds an imbalanced binary problem from the digits data. It compares accuracy with balanced accuracy on the same learning curve.
import numpy as np
from sklearn.datasets import load_digits
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import StratifiedKFold, learning_curve
X, y = load_digits(return_X_y=True)
y_binary = (y == 3).astype(int) # about 10% positives
cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=0)
for metric in ["accuracy", "balanced_accuracy"]:
sizes, train_scores, val_scores = learning_curve(
LogisticRegression(max_iter=2000),
X,
y_binary,
train_sizes=np.linspace(0.1, 1.0, 5),
cv=cv,
scoring=metric,
)
print(metric)
print(" train:", train_scores.mean(axis=1).round(3))
print(" valid:", val_scores.mean(axis=1).round(3))Accuracy will sit near 0.9 or higher even at small sizes, because always predicting "not 3" is already right about 90% of the time. Balanced accuracy averages the recall of each class, so it starts lower and shows how much the model improves as it sees more positive examples.
Negated error metrics
For neg_mean_squared_error, the scores come back as negative numbers. A value of -4.0 is better than -9.0. If you plot these directly, the curve points "up" as the model improves, which matches every other scorer. Negate the values only if you want to display error as a positive quantity, and label the axis accordingly.
Practical guidance
- Pick the metric before you draw curves, based on the cost of mistakes in your application. Do not choose it after seeing which curve looks best.
- Use the same metric for the learning curve, the validation curve, and any later tuning. Otherwise the diagnosis and the final selection can disagree.
- For imbalanced data, combine a class-aware metric with stratified folds so every fold contains some minority samples.
- Threshold-based metrics such as F1 depend on the decision threshold. Ranking metrics such as
roc_aucdo not. Choose based on whether the threshold is fixed in production. - Report the metric name next to every curve you share.
Exercise
A learning curve for a fraud detector shows a validation accuracy of 0.99 at every training size, so a teammate says more data will not help. What is wrong with this conclusion, and what would you change in the call to learning_curve?
Show answer
If fraud is rare, a model that never flags fraud already scores near 0.99 accuracy, so the flat curve says nothing about how well the fraud class is learned. Change scoring to a class-aware metric such as "averageprecision", "f1", or "balancedaccuracy", and use StratifiedKFold so each fold contains fraud cases. Then check whether that curve still rises with more data.
Exercise
You run validationcurve with scoring="negmeansquarederror" and see validation scores of -12.5 at alpha=0.01 and -8.1 at alpha=1. Which alpha is better, and why?
Show answer
alpha=1 is better. Scorers are defined so that higher is better, and -8.1 is greater than -12.5, which means a mean squared error of 8.1 instead of 12.5.