Repository navigation
Description
Introduce yourself
I'm a second-year master's student working on ML for healthcare projects and agentic AI systems.
Describe the bug and give evidence about its user-facing impact
hinge_loss, ndcg_score, dcg_score, coverage_error, label_ranking_loss and other ranking metrics, as well as some modes of roc_auc_score, use sample_weight directly without validating it. All the other metrics validate the weights, but these do not. As a result, they return nan or wrong numbers, which are hard to notice in cross-validation. This happens because the common test does not cover this group of metrics.
Steps/Code to Reproduce
import numpy as np
from sklearn.metrics import hinge_loss, log_loss, ndcg_score, roc_auc_score
rng = np.random.RandomState(0)
y = np.arange(20) % 2
score = rng.uniform(size=20)
Y = rng.randint(0, 2, size=(20, 3))
Y[:, 0] = 1
S = rng.uniform(size=(20, 3))
w = rng.uniform(size=20)
w_inf = w.copy()
w_inf[0] = np.inf
print(log_loss(y, score, sample_weight=w_inf)) # ValueError (reference)
print(hinge_loss(y, score - 0.5, sample_weight=w_inf)) # nan
print(hinge_loss(y, score - 0.5, sample_weight=w + 1j)) # a number
print(ndcg_score(Y, S, sample_weight=w_inf)) # nan
print(roc_auc_score(Y, S, average="weighted", sample_weight=np.zeros(20))) # 0
hinge_loss(y, score - 0.5, sample_weight=np.zeros(20)) # ZeroDivisionErrorExpected Results
The same ValueErrors as the other classification metrics, for example log_loss:
Input sample_weight contains infinity or a value too large for dtype('float64').Input sample_weight contains NaN.Complex data not supportedSample weights must be 1D array or scalar, got 2D array.Sample weights must contain at least one non-zero number.
Actual Results
Summary for each kind of invalid weight (scikit-learn 1.9.1; the code is unchanged on main at 21626d6):
| metric | inf / NaN | complex | 2D | all zeros |
|---|---|---|---|---|
hinge_loss |
returns nan |
returns a number | TypeError |
ZeroDivisionError |
top_k_accuracy_score |
returns nan |
returns a number | TypeError |
ZeroDivisionError |
coverage_error |
returns nan |
returns a number | TypeError |
ZeroDivisionError |
label_ranking_loss |
returns nan |
returns a number | TypeError |
ZeroDivisionError |
label_ranking_average_precision_score |
returns nan |
returns a number | TypeError |
returns nan |
dcg_score |
returns nan |
returns a number | TypeError |
ZeroDivisionError |
ndcg_score |
returns nan |
returns a number | TypeError |
ZeroDivisionError |
roc_auc_score(average="samples") |
returns nan |
ValueError |
ValueError |
ZeroDivisionError |
roc_auc_score(average="weighted") |
ValueError |
ValueError |
ValueError |
returns 0 |
average_precision_score(average="samples") |
returns nan |
ValueError |
ValueError |
ZeroDivisionError |
The TypeError is Axis must be specified when shapes of a and weights differ. and the ZeroDivisionError is Weights sum to zero, can't be normalized, both from np.average.
Versions
System:
python: 3.13.18 (main, Oct 26 2026, 14:08:19) [Clang 23.1.3 ]
machine: Linux-6.18.44-fc-v726-x86_64-with-glibc2.39
Python dependencies:
sklearn: 1.9.1
pip: 26.2.1.post1
setuptools: 84.0.0
numpy: 2.5.3
scipy: 1.18.1
Cython: None
pandas: 3.0.5
matplotlib: 3.11.2
joblib: 1.6.0
threadpoolctl: 3.7.0
narwhals: 2.27.1
Built with OpenMP: TrueInterest in fixing the bug
I would like to fix this. The approach I have in mind:
- call
_check_sample_weight(sample_weight, ..., force_float_dtype=False)whensample_weightis notNonein these functions, as_check_targetsalready does for the other classification metrics; - add the same check in
_average_binary_score, which covers the"samples"and"weighted"paths ofroc_auc_scoreandaverage_precision_score; - add a common test that runs the invalid
sample_weightchecks overCONTINUOUS_CLASSIFICATION_METRICS.
I have a draft with these changes, and the sklearn/metrics test suite passes with it on 1.9.1. I will wait for triage before opening a PR.
Activity
Anish8800 commentedon Nov 22, 2026
Hi! I'd be interested in contributing to this issue and helping implement a fix.
I've gone through the reproduction examples and the proposed approach involving
_check_sample_weightand the common validation tests. I'd like to investigate the affected metrics, understand how the existing sample-weight validation works in scikit-learn, and work on the implementation and regression tests.I'm relatively new to contributing to scikit-learn, so I'd appreciate any guidance regarding the scope of the issue and the proposed approach. If this issue is open for contributions, I'd be happy to work on it.
Jeremy891102 commentedon Nov 22, 2026
Thanks for your interest! I already have a fix with tests ready (described in the issue), and I'll open the PR as soon as the issue is triaged.
Anish8800 commentedon Nov 22, 2026
Thanks for letting me know! I’ll explore other issues in the meantime. Good luck with your PR!
betatim commentedon Nov 23, 2026
It is probably worth considering #32212 as well when working on this. From the top comment the listing under "expected results" sound reasonable. However ,I"ve not spent the time to make sure that this consistency is not going to cause other problems (often consistency is good, but sometimes inconsistency is better for usability).
I'm ok with a PR fixing this to make things more consistent. From my perspective this is a low priority work as you have to be a bit unlucky to hit this problem in practice. This means that it might take a while for someone to review a PR. The better the PR the higher the chances of it being reviewed.
As a side note: make sure to read our contribution policy about automated contributions, which include the use of AI.