Skip to content

FIX Validate sample_weight in ranking metrics and hinge_loss - #35512

Open
Jeremy891102 wants to merge 8 commits into
scikit-learn:mainfrom
Jeremy891102:fix-continuous-metrics-sample-weight
Open

Jeremy891102 wants to merge 8 commits into
scikit-learn:mainfrom
Jeremy891102:fix-continuous-metrics-sample-weight

Conversation

@Jeremy891102

Copy link
Copy Markdown

Reference Issues/PRs

Fixes #35487. See also #32212.

What does this implement/fix? Explain your changes.

When sample_weight is given, the metrics listed in #35487 now call _check_sample_weight to validate it. They were missed before because they don't go through _check_targets, so the change in #32212 did not cover them. I added a common test that checks the invalid weights for all of CONTINUOUS_CLASSIFICATION_METRICS. Before this change, they returned nan or 0, or raised unrelated errors; now they raise the same ValueError as the other metrics. The behavior for valid weights does not change.

Introduce yourself

I'm a second-year master's student working on ML for healthcare projects and agentic AI systems.

AI usage disclosure

I used AI assistance for:

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Research and understanding

Any other comments?

@github-actions

Copy link
Copy Markdown

Thank you for opening your first pull request to scikit-learn! 🎉

To help get your contribution reviewed, please make sure that:

  • You have filled out the pull request template.

  • The pull request addresses an existing issue that is ready for contribution (e.g. not tagged as 'Needs Triage', 'Needs Decision', ...). If you are proposing a new feature, please open an issue to discuss it first.

  • There are no other open pull requests already targeting the same issue.

  • You have followed the pull request checklist. In particular, linting and tests should pass.

@Jeremy891102

Copy link
Copy Markdown
Author

CI failed at first because test_multiclass_ovr_roc_auc_zero_sample_weight, added in #33200, expects 0.0 when all sample_weight values are zero.
Following #32212, this PR now raises a ValueError for all-zero weights, consistent with the other metrics.
So I updated that test to expect a ValueError. It still guards against the original AttributeError crash.
If you would prefer roc_auc_score to keep returning 0.0 for all-zero weights, I can change it back.

@betatim

betatim commented Nov 25, 2026

Copy link
Copy Markdown
Member

cc @virchan who worked on #33200

@virchan virchan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR, @Jeremy891102.

CONTINUOUS_CLASSIFICATION_METRICS is not a subset of CLASSIFICATION_METRICS. Therefore, the new test is independent of the existing test_classification_with_invalid_sample_weight, and seems appropriate.

I also have a few comments below. Otherwise, LGTM.

Comment thread sklearn/metrics/tests/test_ranking.py Resolved
Comment thread sklearn/metrics/_base.py
Comment on lines +87 to +90
if sample_weight is not None:
sample_weight = _check_sample_weight(
sample_weight, y_score, force_float_dtype=False
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move this a few blocks earlier. Ideally, all input validation should be completed before calling move_to.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Several ranking metrics and hinge_loss do not validate sample_weight

3 participants