Repository navigation
ENH Auto-requesting sample_weight on pure consumers - #34966
Conversation
Co-authored-by: Stefanie Senger <[email protected]>
Co-authored-by: antoinebaker <[email protected]>
…n into default_routing3
antoinebaker
left a comment
There was a problem hiding this comment.
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.
| return func | ||
|
|
||
|
|
||
| class AutoRequestMixin: |
There was a problem hiding this comment.
Should we name it AutoRequestSampleWeightMixin as only sample_weight are auto requested ?
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
Thank you @antoinebaker! :) I have addressed all your comments.
| return func | ||
|
|
||
|
|
||
| class AutoRequestMixin: |
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
Hi @StefanieSenger, LGTM overall !
| @@ -0,0 +1,6 @@ | |||
| - |Feature| scikit-learn estimators that inherit from | |||
There was a problem hiding this comment.
I guess you'll have the tag duplication.
There was a problem hiding this comment.
This is true, fixed it.
| 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 | ||
| ) |
There was a problem hiding this comment.
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 NoneThere was a problem hiding this comment.
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:]) |
There was a problem hiding this comment.
Just for my own understanding: this last condition is to select classes that inherit from BaseEstimator ?
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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 | |||
There was a problem hiding this comment.
This is true, fixed it.
| 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 | ||
| ) |
There was a problem hiding this comment.
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:]) |
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
A couple of nitpicks, but otherwise, LGTM, thanks.
| from sklearn.utils._metadata_requests import ( | ||
| _MetadataRequester, | ||
| _routing_enabled, | ||
| ) |
There was a problem hiding this comment.
| 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.
There was a problem hiding this comment.
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).
| if ( | ||
| hasattr(self, "_auto_request_sample_weight") | ||
| and self._auto_request_sample_weight | ||
| ): |
There was a problem hiding this comment.
| 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 ) ;)
There was a problem hiding this comment.
I love it! 😍 Thank you!
StefanieSenger
left a comment
There was a problem hiding this comment.
Many thanks, @FrancoisPgm! I appreciate your input. :)
| if ( | ||
| hasattr(self, "_auto_request_sample_weight") | ||
| and self._auto_request_sample_weight | ||
| ): |
There was a problem hiding this comment.
I love it! 😍 Thank you!
| from sklearn.utils._metadata_requests import ( | ||
| _MetadataRequester, | ||
| _routing_enabled, | ||
| ) |
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
LGTM. Thank you @StefanieSenger
|
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). |
|
Unrelated follow-up PR: MultiOutput* and RANSACRegressor still have They qualify for an auto-request, but don't realise it because they don't add a self_request. We may consider deprecating the |
…eady covers this
Reference Issues/PRs
towards #34901
What does this implement/fix? Explain your changes.
This adds auto-requests for
sample_weightto all estimators that inherit fromBaseEstimatorand 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
AutoRequestMixinto be used by third party libraries if they want to opt into auto-requestingsample_weight.Auto-requesting
sample_weighton self-consuming routers will be a different PR (#35095)AI usage disclosure