Skip to content

ENH Auto-requesting sample_weight on pure consumers - #34966

Merged
OmarManzoor merged 68 commits into
scikit-learn:mainfrom
StefanieSenger:auto-request_sample_weight
Oct 14, 2026
Merged

OmarManzoor merged 68 commits into
scikit-learn:mainfrom
StefanieSenger:auto-request_sample_weight

Conversation

@StefanieSenger

@StefanieSenger StefanieSenger commented Sep 15, 2026 •

Copy link
Copy Markdown
Member

Reference Issues/PRs

towards #34901

What does this implement/fix? Explain your changes.

This adds auto-requests for sample_weight to all estimators that inherit from BaseEstimator and tests.
This is done by inspecting the module name upon class creation (so we don't need to add an AutoRequestMixin into the mro of all our estimators.)

This PR also adds AutoRequestMixin to be used by third party libraries if they want to opt into auto-requesting sample_weight.

Auto-requesting sample_weight on self-consuming routers will be a different PR (#35095)

AI usage disclosure

  • Research and understanding
  • code generation

adrinjalali and others added 30 commits February 27, 2025 16:22
@StefanieSenger StefanieSenger added the Metadata Routing all issues related to metadata routing, slep006, sample props label Oct 7, 2026

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

Thanks @StefanieSenger for the PR ! LGTM so far :)

If I understand correctly, your main concern was to have sample weight auto requested for sklearn estimators only, without forcing third party developers ? They can still opt in with the Mixin.

Comment thread sklearn/tests/test_base.py Resolved
Comment thread sklearn/tests/test_base.py Resolved
Comment thread sklearn/tests/test_pipeline.py Resolved
Comment thread sklearn/tests/test_pipeline.py Resolved
Comment thread sklearn/tests/test_pipeline.py Resolved
return func


class AutoRequestMixin:

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.

Should we name it AutoRequestSampleWeightMixin as only sample_weight are auto requested ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hm, I thought AutoRequestMixin would be most flexible. We could later consider adding groups to it, in case we want to do the deprecation of GroupKFold and StratifiedGroupKFold without needing a new Mixin. Users could also expand on it.

@StefanieSenger StefanieSenger left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thank you @antoinebaker! :) I have addressed all your comments.

Comment thread sklearn/tests/test_pipeline.py Resolved
return func


class AutoRequestMixin:

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hm, I thought AutoRequestMixin would be most flexible. We could later consider adding groups to it, in case we want to do the deprecation of GroupKFold and StratifiedGroupKFold without needing a new Mixin. Users could also expand on it.

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

Hi @StefanieSenger, LGTM overall !

@@ -0,0 +1,6 @@
- |Feature| scikit-learn estimators that inherit from

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.

I guess you'll have the tag duplication.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is true, fixed it.

Comment thread sklearn/tests/test_base.py Outdated
Comment on lines +1153 to +1167
for method in SIMPLE_METHODS:
if method == "fit":
assert (
getattr(get_routing_for_object(est), method).requests.get(
"sample_weight"
)
is True
)
else:
assert (
getattr(get_routing_for_object(est), method).requests.get(
"sample_weight"
)
is None
)

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.

I would reduce the nesting a bit to make the code more compact

routing = get_routing_for_object(est)
assert routing.fit.requests.get("sample_weight") is True
methods = [m for m in SIMPLE_METHODS if m != "fit"]
for method in methods:
    assert getattr(routing, method).requests.get("sample_weight") is None

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This looks so much nicer. Thanks a lot!

if (
cls.__module__.startswith("sklearn.")
and "_auto_request_sample_weight" not in cls.__dict__
and any(base.__name__ == "BaseEstimator" for base in cls.__mro__[1:])

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.

Just for my own understanding: this last condition is to select classes that inherit from BaseEstimator ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, this is to select any scikit-learn class that inherits from BaseEstimator but is not itself BaseEstimator. I've tried to improve the comment above for future maintenance.

@StefanieSenger StefanieSenger left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks for you review, @antoinebaker! Happy this is moving forwards.

@OmarManzoor, would you be interested to also have a look into this?

@@ -0,0 +1,6 @@
- |Feature| scikit-learn estimators that inherit from

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is true, fixed it.

Comment thread sklearn/tests/test_base.py Outdated
Comment on lines +1153 to +1167
for method in SIMPLE_METHODS:
if method == "fit":
assert (
getattr(get_routing_for_object(est), method).requests.get(
"sample_weight"
)
is True
)
else:
assert (
getattr(get_routing_for_object(est), method).requests.get(
"sample_weight"
)
is None
)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This looks so much nicer. Thanks a lot!

if (
cls.__module__.startswith("sklearn.")
and "_auto_request_sample_weight" not in cls.__dict__
and any(base.__name__ == "BaseEstimator" for base in cls.__mro__[1:])

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, this is to select any scikit-learn class that inherits from BaseEstimator but is not itself BaseEstimator. I've tried to improve the comment above for future maintenance.

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

A couple of nitpicks, but otherwise, LGTM, thanks.

Comment thread sklearn/base.py Outdated
Comment on lines +20 to +23
from sklearn.utils._metadata_requests import (
_MetadataRequester,
_routing_enabled,
)

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.

Suggested change
from sklearn.utils._metadata_requests import (
_MetadataRequester,
_routing_enabled,
)
from sklearn.utils._metadata_requests import _MetadataRequester, _routing_enabled

The refactoring does not seem necessary, as in one line it does not get longer than the 88 characters limit. I guess this is some leftover from a previous version with more imports.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, this is a leftover.
Let's put everything into one line to check if ruff accepts this or not (since it may still alter files that I had touched and sometimes it does unexpected things).

Comment thread sklearn/utils/_metadata_requests.py Outdated
Comment on lines +1896 to +1899
if (
hasattr(self, "_auto_request_sample_weight")
and self._auto_request_sample_weight
):

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.

Suggested change
if (
hasattr(self, "_auto_request_sample_weight")
and self._auto_request_sample_weight
):
if getattr(self, "_auto_request_sample_weight", False):

A more concise version that does the same (I learned from @jeremiedbb ) ;)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I love it! 😍 Thank you!

@StefanieSenger StefanieSenger left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Many thanks, @FrancoisPgm! I appreciate your input. :)

Comment thread sklearn/utils/_metadata_requests.py Outdated
Comment on lines +1896 to +1899
if (
hasattr(self, "_auto_request_sample_weight")
and self._auto_request_sample_weight
):

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I love it! 😍 Thank you!

Comment thread sklearn/base.py Outdated
Comment on lines +20 to +23
from sklearn.utils._metadata_requests import (
_MetadataRequester,
_routing_enabled,
)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, this is a leftover.
Let's put everything into one line to check if ruff accepts this or not (since it may still alter files that I had touched and sometimes it does unexpected things).

@OmarManzoor OmarManzoor left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM. Thank you @StefanieSenger

@OmarManzoor
OmarManzoor enabled auto-merge (squash) October 14, 2026 12:36
@OmarManzoor
OmarManzoor merged commit d0e22ae into scikit-learn:main Oct 14, 2026
35 checks passed
@github-project-automation github-project-automation Bot moved this from PR waiting for reviews to Done in Labs Oct 14, 2026
@StefanieSenger

StefanieSenger commented Oct 15, 2026 •

Copy link
Copy Markdown
Member Author

Thanks everyone! 🫶 I am very happy we got this one in.

We can still add to this or refactor in follow-up PRs, @adrinjalali, while keeping the API fixed as it had been defined in #31413.

FYI: 23rd of October is the last day I will be able to work on auto-requests before the release (on vacation afterwards).

@StefanieSenger

Copy link
Copy Markdown
Member Author

Unrelated follow-up PR: MultiOutput* and RANSACRegressor still have sample_weight in their signatures for historical reasons (but are pure routers). For instance fit(..., sample_weight=None, **fit_params).

They qualify for an auto-request, but don't realise it because they don't add a self_request.

We may consider deprecating the sample_weight param and steer users to use **fit_params` instead. These could be cleanup PRs.

StefanieSenger added a commit to StefanieSenger/scikit-learn that referenced this pull request Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Metadata Routing all issues related to metadata routing, slep006, sample props module:utils

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants