Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

MNT Make sample_weight checking more consistent in regression metrics #30886

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 11 commits into from
May 29, 2025

Conversation

lucyleeow
Copy link
Member

Reference Issues/PRs

Ref: #30787 (comment)

What does this implement/fix? Explain your changes.

_check_reg_targets will now perform check_consistent_length on sample_weight as well as y_true and y_pred as well as _check_sample_weight

  • this means that all array checks are done _check_reg_targets, which means all checks are at the start and means we know who is raising errors relating to inputs
  • only 2 metrics performed _check_sample_weight but AFAICT other metrics that accept sample_weight would also benefit from this check

Any other comments?

Not sure of what extra tests to add as _check_sample_weight and check_consistent_length are both tested separately, and it seems redundant to check those again in the context of _check_reg_targets.

I guess I could try a few different inputs and check that the result is the same as what _check_sample_weight gives ?

cc @ogrisel

Copy link

github-actions bot commented Feb 24, 2025

✔️ Linting Passed

All linting checks passed. Your pull request is in excellent shape! ☀️

Generated for commit: a5ddd5e. Link to the linter CI: here

@ogrisel
Copy link
Member

ogrisel commented Feb 24, 2025

@lucyleeow there are broken tests with older versions of numpy. Can you please take a look?

@lucyleeow
Copy link
Member Author

@ogrisel welp the tests now pass and the CI workflows are so old I can't see them, so I don't know what the previous failure was exactly.

Are you happy with the status @ogrisel or ?

I am not sure this requires a changelog entry. Technically we are adding _check_sample_weight to some metrics, which previously did not have this check. AFAICT the additional checks that users would affect users would be:

  • sample_weight can only be 1D or scalar
  • sample_weight cannot be negative

I can add changelogs to all metrics where we added _check_sample_weight to explain the additional checking?

@lucyleeow
Copy link
Member Author

@ogrisel gentle ping 🙏

@lucyleeow
Copy link
Member Author

lucyleeow commented May 15, 2025

@ogrisel I am thinking of making similar changes to _check_reg_targets - it seems reasonable that _check_sample_weight can be performed in there. It also ensures that check_consistent_length is always performed with y_true, y_pred AND sample_weight, if present, for regression metrics.

But it's probably best to get the okay here first, so gentle ping again 😬

Copy link
Member

@ogrisel ogrisel left a comment

Choose a reason for hiding this comment

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

Sorry for the lack of reaction on previous pings. Could you please just add a new test (e.g. test_regression_invalid_sample_weight) in sklearn/metrics/tests/test_common.py with a parametrization similar to the existing test_regression_sample_weight_invariance test to quickly check that all the functions mentioned in the changelog entry actually raise the right exception on invalid sample_weight values?

@ogrisel
Copy link
Member

ogrisel commented May 15, 2025

@ogrisel I am thinking of making similar changes to _check_reg_targets - it seems reasonable that _check_sample_weight can be performed in there.

I don't understand this comment. This is precisely what is already implemented in this PR. Did you mean to do something similar for another help function besides _check_reg_targets? If so, which one?

I took a quick look at classification metrics and while some factor input validation logic into the _validate_multiclass_probabilistic_prediction helper, the non-probabilistic metrics do a lot of manual check that could probably be factorized to be more consistent.

@lucyleeow
Copy link
Member Author

I don't understand this comment. This is precisely what is already implemented in this PR.

🤦 I've forgotten what this PR does (the give away was _check_reg_targets). I meant for classification metrics, updating _check_targets to do the check_consistent_length and sample weight check for classification metrics.

@ogrisel
Copy link
Member

ogrisel commented May 15, 2025

+1 for a follow-up PR for classification metrics then.

@ogrisel
Copy link
Member

ogrisel commented May 15, 2025

Labeling this as array API related, as it will make it easier to use _check_sample_weight in array API PRs (related to metrics or not).

cc @OmarManzoor @StefanieSenger @lesteve @betatim.

@lucyleeow
Copy link
Member Author

Thanks @ogrisel ! Added a test_regression_invalid_sample_weight

Copy link
Contributor

@OmarManzoor OmarManzoor left a comment

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 @lucyleeow
A few comments otherwise looks nice

@@ -0,0 +1,15 @@
- Additional `sample_weight` checking has been added to
:func:`metrics.mean_absolute_error`, :func:`metrics.mean_pinball_loss`
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
:func:`metrics.mean_absolute_error`, :func:`metrics.mean_pinball_loss`
:func:`metrics.mean_absolute_error`,
:func:`metrics.mean_pinball_loss`,

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks. Reading this again made me realise I was wrong about non-negative checks further down. _check_sample_weight does have a ensure_non_negative added in 1.0 but it was turned off for the 2 regression metrics that currently (on main) use _check_sample_weight.

I've kept this behaviour in this PR but now I think about it, I don't think negative weights make sense (after doing some reading on types of weights and their meaning e.g., sampling/probability weights, analytical weights, frequency weights). I think we should turn on ensure_non_negative for all regression metrics. WDYT?

cc also @ogrisel

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree that negative sample weights don't make much sense but please check this issue:
#12464

Let's wait for the opinion of @ogrisel

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, I didn't know about that thread, it seems to be a more contentious topic than I originally thought. I would vote for leaving that decision out of this PR.

Copy link
Member

@betatim betatim May 16, 2025

Choose a reason for hiding this comment

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

The only place I've met negative sample weights is in particle physics. They appear in a particular kind of Monte Carlo generator where they simulate some quantum mechanical process where sometimes you get destructive interference and hence want to "subtract" an event. The simple way to use these weights is when you, for example, make a plot of the angular distribution of particles generated in a collision at something like the Large Hadron Collider. When filling the histogram of the angular distribution you use the positive sample weights to increase the content of the corresponding bin. And if the weight is negative you subtract the sample weight from the bin contents. At the end you get the correct angular distribution (the shape of it would be wrong if you didn't subtract things).

Long story short, I think how to treat these samples with negative weights for anything other than histogram'ing is a bit of a mystery (an issue from a previous order of magnitude of issue numbers! :-o), or at least it was ~10yrs ago when I last worked on it.

I'm not sure if we need to forbid them, a long time ago we made an effort with some people from particle physics to try and accommodate their use-case in scikit-learn. But I think we didn't get suuuper far (because it is so tricky/mind bending).

sklearn/metrics/_regression.py Outdated Show resolved Hide resolved
sklearn/metrics/tests/test_regression.py Outdated Show resolved Hide resolved
sklearn/metrics/tests/test_regression.py Outdated Show resolved Hide resolved
sklearn/metrics/tests/test_common.py Outdated Show resolved Hide resolved
sklearn/metrics/tests/test_common.py Outdated Show resolved Hide resolved
@lucyleeow
Copy link
Member Author

Thanks @OmarManzoor , changes made. It has raised a question about negative weights though.

Copy link
Member

@betatim betatim left a comment

Choose a reason for hiding this comment

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

LGTM 🎉 Thanks for the improvement

@lucyleeow lucyleeow added Waiting for Second Reviewer First reviewer is done, need a second one! and removed Waiting for Second Reviewer First reviewer is done, need a second one! labels May 19, 2025
@lucyleeow
Copy link
Member Author

Lets ignore the negative weights. Are you happy for this to go in @ogrisel / @betatim ?

@lucyleeow
Copy link
Member Author

Gentle ping @betatim @ogrisel , could this go in?

@ogrisel ogrisel merged commit bff3d7d into scikit-learn:main May 29, 2025
41 checks passed
@lucyleeow lucyleeow deleted the reg_metrics_checks branch May 29, 2025 08:38
fkiraly pushed a commit to sktime/sktime that referenced this pull request Jun 26, 2025
… 1.7 #8457 (#8459)

#### Reference Issues/PRs
Fixes #8457. 

#### What does this implement/fix? Explain your changes.
Copies the internal v1.6 [_check_reg_targets from
sklearn](https://github.com/scikit-learn/scikit-learn/blob/main/sklearn/metrics/_regression.py#L61)
into sktime.utils.sklearn._metrics. This is a hotfix for forecasting
functions not being compatible with sklearn v1.7 which changed
_check_reg_targets' signature in this
[MR](scikit-learn/scikit-learn#30886.). See
discussion on issue on the longer term fix.

This short term fix is probably worth it since _check_reg_targets is
called at the start of most forecasting._functions and there is no
workaround or kwarg that you can give to workaround the issue. It is a
hard TypeError in the signature.

#### What should a reviewer concentrate their feedback on?

This is a hotfix. I do not love the approach but I do not have time for
a more principled approach.


#### Did you add any tests for the change?

The existing tests should now catch this since the module
performance_metrics has changed. I am not happy that this was not
detected in the unit tests which is the actual cause of the bug - we
skip these performance metrics entirely if the module hasn't changed
from our side using our pytest `run_test_module_changed` mark. This
should ideally also rerun if dependencies changed but is outside the
scope of this MR.
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.

4 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.