Repository navigation
fix(metrics): pass the probability columns as labels in LogLoss - #518
Merged
Merged
Conversation
sklearn's log_loss infers the classes from y_true, so LogLoss raised for any evaluation set that lacks a class: as a validation metric it stopped fit(), and evaluate() reported NaN. For a 2-D probability matrix and integer class indices, pass labels=0..K-1 so the score is defined for any subset of observed classes. Fixes #517
Integer labels outside 0..K-1 (for example classes 1, 2, 3) are raw class labels, not column indices. Passing labels=0..K-1 for them made scikit-learn score the wrong columns without a warning, so they are left to scikit-learn's class inference again. Refs #517
mkumar73
approved these changes
Oct 31, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #517
Problem
LogLosscalledsklearn.metrics.log_loss(y_true, y_pred)withoutlabels. scikit-learn then infers the classes fromy_trueand raises when the probability matrix has more columns than observed classes. Any validation or test set that lacks a class therefore:fit()whenLogLosswas a training/validation metric;log_loss = naninevaluate(), whereLogLossis one of the default classification metrics.Fix
For a 2-D probability matrix with more than one column and integer labels that are all valid column indices (
0 <= y < K),LogLosspasseslabels=np.arange(K). The probability columns are ordered by the encoded classes0..K-1, as in the training loop andpredict_proba(). Other inputs (1-D positive-class probabilities, non-integer labels, and integers outside0..K-1such as raw labels{1, 2, 3}) are passed to scikit-learn unchanged, which infers the classes fromy_trueas before. The class docstring documents this.Merge after #494.
evaluate()andscore(..., metric=(LogLoss(), True))pass raw labels to probability metrics onmain(#493). Raw integer labels that lie inside0..K-1without being the encoded indices (e.g.{1, 2}from classes{1, 2, 3}) can't be told apart from encoded ones here; with #494 these callers pass encoded labels, so the scores are correct in every case.Tests
tests/metrics/test_classification_metrics.py::TestLogLossWithMissingClasses:test_multiclass_matrix_uses_all_columns_as_classestest_binary_matrix_with_a_single_observed_classtest_evaluate_and_validation_metrics_on_data_without_one_class: anMLPClassifierwithval_metrics={"log_loss": LogLoss()}and a validation set lacking class 2 fits, andevaluate()on data without class 2 returns a finite log loss.test_labels_outside_the_column_indices_are_inferred_by_sklearn:LogLoss()([1, 2, 3], P)equalslog_loss([1, 2, 3], P).test_evaluate_with_labels_other_than_column_indices: for anMLPClassifiertrained on labels{1, 2, 3},evaluate()equalslog_loss(y, predict_proba(X), labels=classes_).The first three fail on
mainand pass with this change. The last two pass onmaintoo; they guard against scoring raw labels as column indices.Docs
The
LogLossdocstring, shown in the API reference, describes the behaviour. No page needed a change.Checks
just test: 3241 passed, 46 skipped, 28 xfailed; coverage 91.32%just docs: build succeeded (warnings treated as errors)just check: all hooks passed (ruff, prettier, pyright)