log_loss
gridsearchcv
scoring
machine learning
sklearn

How to use log_loss scorer in gridsearchcv?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

When tuning probabilistic classifiers, accuracy is often too coarse because it ignores how confident the model was. Log loss is better suited to that job, and in scikit-learn the usual way to use it with GridSearchCV is to score with the built-in metric name neg_log_loss.

Why the Name Is Negative

GridSearchCV assumes that higher scores are better. Log loss is the opposite: smaller values are better. To make the scoring API consistent, scikit-learn exposes log loss as its negative value.

That means:

  • better models have scores closer to zero
  • a score of -0.20 is better than -0.60

This naming convention is the part that confuses people most often.

The Simplest Setup

If your estimator supports predict_proba, you can pass the scoring name directly.

python
1from sklearn.datasets import load_breast_cancer
2from sklearn.linear_model import LogisticRegression
3from sklearn.model_selection import GridSearchCV
4
5X, y = load_breast_cancer(return_X_y=True)
6
7model = LogisticRegression(max_iter=2000)
8param_grid = {
9    "C": [0.01, 0.1, 1.0, 10.0],
10    "solver": ["lbfgs"],
11}
12
13search = GridSearchCV(
14    estimator=model,
15    param_grid=param_grid,
16    scoring="neg_log_loss",
17    cv=5,
18    n_jobs=-1,
19)
20
21search.fit(X, y)
22
23print("Best params:", search.best_params_)
24print("Best score:", search.best_score_)
25print("Best log loss:", -search.best_score_)

The final line flips the sign back so you can read the result as ordinary log loss.

Your Model Must Provide Probabilities

Log loss compares the true labels against predicted probabilities, not just predicted classes. That means your estimator must implement predict_proba, or in some cases a compatible decision output that the scorer can use.

For example, logistic regression, random forests, and gradient boosting classifiers usually work well here. A classifier without probability estimates is not a good fit for log-loss scoring unless you wrap or calibrate it.

Using a Custom Scorer

If you want explicit control, you can build a scorer with make_scorer. This is useful when you want to fix class labels or scorer behavior manually.

python
1from sklearn.metrics import log_loss, make_scorer
2
3log_loss_scorer = make_scorer(
4    log_loss,
5    response_method="predict_proba",
6    greater_is_better=False,
7)

Then pass log_loss_scorer into GridSearchCV as the scoring argument.

In many cases, though, the built-in string "neg_log_loss" is simpler and less error-prone.

Multi-Class Models Work Too

Log loss is not limited to binary classification. For multi-class problems, predict_proba should return one probability per class, and scikit-learn computes the multi-class version of log loss automatically.

python
1from sklearn.datasets import load_iris
2from sklearn.ensemble import RandomForestClassifier
3from sklearn.model_selection import GridSearchCV
4
5X, y = load_iris(return_X_y=True)
6
7search = GridSearchCV(
8    RandomForestClassifier(random_state=42),
9    param_grid={"n_estimators": [50, 100], "max_depth": [None, 3, 5]},
10    scoring="neg_log_loss",
11    cv=5,
12)
13
14search.fit(X, y)
15print(-search.best_score_)

Common Pitfalls

The biggest mistake is forgetting that scores are negative. Developers see -0.42 and think the model is bad because the metric is below zero, when the score is simply the negated log loss.

Another pitfall is using a model that does not provide probabilities. In that case, GridSearchCV may fail during scoring, or you may end up tuning the wrong kind of estimator for the metric.

Class imbalance matters too. Log loss penalizes overconfident mistakes heavily, which is usually desirable, but it also means calibration problems can dominate the score even when accuracy looks good.

Finally, do not compare best_score_ directly to standalone log_loss output without flipping the sign. One is negative by convention; the other is the ordinary positive loss value.

Summary

  • Use scoring="neg_log_loss" in GridSearchCV for probability-based model selection.
  • The score is negative because scikit-learn expects higher values to be better.
  • Convert it back with -search.best_score_ when you want the usual log-loss number.
  • Make sure the estimator supports predict_proba or an equivalent probability response.
  • Prefer the built-in scoring string unless you need a custom scorer for a special case.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.