Skip to content

Error in d2_log_loss_score multiclass when one of the classes is missing in y_true. #30903

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/whats_new/upcoming_changes/sklearn.metrics/30903.fix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- :func:`~metrics.d2_log_loss_score` now properly handles the case when `labels` is
passed and not all of the labels are present in `y_true`.
By :user:`Vassilis Margonis <vmargonis>`
15 changes: 13 additions & 2 deletions sklearn/metrics/_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -3690,8 +3690,19 @@ def d2_log_loss_score(y_true, y_pred, *, sample_weight=None, labels=None):
# Proportion of labels in the dataset
weights = _check_sample_weight(sample_weight, y_true)

_, y_value_indices = np.unique(y_true, return_inverse=True)
counts = np.bincount(y_value_indices, weights=weights)
# If labels is passed, augment y_true to ensure that all labels are represented
# Use 0 weight for the new samples to not affect the counts
y_true_, weights_ = (
(
np.concatenate([y_true, labels]),
np.concatenate([weights, np.zeros_like(weights, shape=len(labels))]),
)
if labels is not None
else (y_true, weights)
)

_, y_value_indices = np.unique(y_true_, return_inverse=True)
counts = np.bincount(y_value_indices, weights=weights_)
y_prob = counts / weights.sum()
y_pred_null = np.tile(y_prob, (len(y_true), 1))

Expand Down
40 changes: 40 additions & 0 deletions sklearn/metrics/tests/test_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -3316,6 +3316,46 @@ def test_d2_log_loss_score():
assert d2_score < 0


def test_d2_log_loss_score_missing_labels():
"""Check that d2_log_loss_score works when not all labels are present in y_true

non-regression test for https://github.com/scikit-learn/scikit-learn/issues/30713
"""
y_true = [2, 0, 2, 0]
labels = [0, 1, 2]
sample_weight = [1.4, 0.6, 0.7, 0.3]
y_pred = np.tile([1, 0, 0], (4, 1))

log_loss_obs = log_loss(y_true, y_pred, sample_weight=sample_weight, labels=labels)

# Null model consists of weighted average of the classes.
# Given that the sum of the weights is 3,
# - weighted average of 0s is (0.6 + 0.3) / 3 = 0.3
# - weighted average of 1s is 0
# - weighted average of 2s is (1.4 + 0.7) / 3 = 0.7
y_pred_null = np.tile([0.3, 0, 0.7], (4, 1))
log_loss_null = log_loss(
y_true, y_pred_null, sample_weight=sample_weight, labels=labels
)

expected_d2_score = 1 - log_loss_obs / log_loss_null
d2_score = d2_log_loss_score(
y_true, y_pred, sample_weight=sample_weight, labels=labels
)
assert_allclose(d2_score, expected_d2_score)


def test_d2_log_loss_score_label_order():
"""Check that d2_log_loss_score doesn't depend on the order of the labels."""
y_true = [2, 0, 2, 0]
y_pred = np.tile([1, 0, 0], (4, 1))

d2_score = d2_log_loss_score(y_true, y_pred, labels=[0, 1, 2])
d2_score_other = d2_log_loss_score(y_true, y_pred, labels=[0, 2, 1])

assert_allclose(d2_score, d2_score_other)


def test_d2_log_loss_score_raises():
"""Test that d2_log_loss_score raises the appropriate errors on
invalid inputs."""
Expand Down