Skip to content

FIX Handle zero-weight samples in regression scores - #34798

Closed
aswanth-07 wants to merge 2 commits into
scikit-learn:mainfrom
aswanth-07:fix/regression-effective-sample-count
Closed

aswanth-07 wants to merge 2 commits into
scikit-learn:mainfrom
aswanth-07:fix/regression-effective-sample-count

Conversation

@aswanth-07

Copy link
Copy Markdown

Reference Issues/PRs

No existing issue. I reproduced this on current main and found no matching open or closed issue or pull request.

What does this implement/fix? Explain your changes.

Regression scores document that they are undefined for fewer than two samples, and scikit-learn's common metric contract treats zero-weight samples as equivalent to removing them. The existing guards counted the raw input length, so one nonzero-weight sample plus any number of zero-weight samples bypassed the guard. In particular, d2_tweedie_score could then raise ZeroDivisionError, while the other affected scores returned a misleading finite value.

This change counts nonzero sample weights after validation when deciding whether r2_score, d2_tweedie_score, and d2_pinball_score have enough observations. d2_absolute_error_score inherits the corrected behavior through d2_pinball_score. All four now follow the existing single-sample path: emit UndefinedMetricWarning and return NaN.

Validation:

  • python -m pytest sklearn/metrics/tests/test_regression.py -q — 33 passed
  • common regression sample-weight invariance tests — 20 passed
  • focused Array API compliance tests with SCIPY_ARRAY_API=1 — 12 passed (optional backends skipped)
  • Ruff lint and format checks — passed

Introduce yourself

I'm Aswanth. I focus on machine learning and deep learning and contribute bug fixes to scientific Python and ML libraries.

AI usage disclosure

I used AI assistance for:

  • Code generation
  • Test generation
  • Research and understanding

Any other comments?

The permanent regression test fails for all four score functions on pristine main; d2_tweedie_score fails with ZeroDivisionError and the others fail because they do not emit the documented warning.

This pull request includes code written with the assistance of AI.
The code has not yet been reviewed by a human.

@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.

@aswanth-07

Copy link
Copy Markdown
Author

Closing this PR after portfolio audit. scikit-learn's current contribution workflow requires an existing issue that is ready for development, while this PR explicitly has no reference issue. The implementation may be technically plausible, but the policy and demand gate is not met. Please re-file only after issue triage confirms scope.

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.

1 participant