Skip to content

MNT Refactor auto-requests for validation sets - #35147

Open
StefanieSenger wants to merge 4 commits into
scikit-learn:mainfrom
StefanieSenger:refactor_X_val
Open

StefanieSenger wants to merge 4 commits into
scikit-learn:mainfrom
StefanieSenger:refactor_X_val

Conversation

@StefanieSenger

@StefanieSenger StefanieSenger commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

Reference Issues/PRs

towards #34901

What does this implement/fix? Explain your changes.

This is a re-factor of #34957, now we have #34966 merged.

It does not change any API or functionality (and thus isn't urgent)

It only changes the explicitness of auto-requesting validation sets on HGB*. With this PR, any estimator that newly accepts X_val, y_val, ... will expose an auto-request if enable_metadata_auto_requests=True (but we can opt out by setting it to False explicitly if we want to).

The advantage would be that in process_routing, we have one central place where we add all the auto-request setting and we would not need to overwrite the get_metadata_routing() methods of all the estimators that will in future accept validation sets.

AI usage disclosure

I used AI assistance for:

  • Documentation (including examples)
  • Research and understanding

@StefanieSenger StefanieSenger added Metadata Routing all issues related to metadata routing, slep006, sample props and removed module:ensemble module:utils labels Oct 15, 2026
@github-project-automation github-project-automation Bot moved this to Todo in Labs Oct 15, 2026
@StefanieSenger StefanieSenger moved this from Todo to PR waiting for reviews in Labs Oct 15, 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.

@StefanieSenger should we add tests for the X_val, y_val, sample_weight_val auto requests ?

@StefanieSenger

StefanieSenger commented Oct 21, 2026 •

Copy link
Copy Markdown
Member Author

@StefanieSenger should we add tests for the X_val, y_val, sample_weight_val auto requests ?

We certainly can, but if sample_weight is requested the other metadata will be as well. I think that in future we will refactor the auto-request adding in process_routing so that we iterate through a list of metadata strings and surely then we have full coverage.

I'm happy to still all X_val, y_val, and sample_weight_val to the test cases if you want. Should we?

@FrancoisPgm

FrancoisPgm commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

I also think the validation set auto requests should be tested. If not tested, a typo here like if getattr(self, "_auto_request_validatoin_set", False): would be unnoticed for example.

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

Here is some extra feedback beyond the missing test discussed above.

Comment on lines +1929 to +1930
if "sample_weight_val" in method_request.requests:
method_request.add_auto_request("sample_weight_val")

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.

Shouldn't we check that both _auto_request_sample_weight and _auto_request_validation_set are true at the same time before auto requesting "sample_weight_val"?

Suggested change
if "sample_weight_val" in method_request.requests:
method_request.add_auto_request("sample_weight_val")
if (
getattr(self, "_auto_request_sample_weight", False)
and "sample_weight_val" in method_request.requests
):
method_request.add_auto_request("sample_weight_val")

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 am not sure. In scikit-learn we always auto-request both anyway (if they are present in the signature).

Adding this condition would constrain third party estimators, if their developers set _auto_request_sample_weight = False and keep _auto_request_validation_set = True. I'd tend to have them be responsible on how to use these attributes and not create an interdependence between them that we then need to document.

What do you think?

# classes do not set this attribute (even if they inherit from `BaseEstimator`)
# but they can opt in by inheriting from `AutoRequestMixin`.
if cls.__module__.startswith("sklearn.") 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.

Wouldn't issubclass(base, BaseEstimator) work here?

@StefanieSenger StefanieSenger Nov 24, 2026 •

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.

Theoretically I think it should work, but if I try I get a nasty circular import error upon from sklearn.base import BaseEstimator because these two modules import each other. We could move the attribute creation (cls._auto_request_sample_weight = True and cls._auto_request_validation_set = True) into BaseEstimator to circumvent this, but then the auto-request setup is spread across different classes and I would rather leave it as is.

@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 your reviews, @antoinebaker, @FrancoisPgm and @ogrisel.
I have added the test cases for validation sets into sklearn/tests/test_base.py and replied to the open questions. Happy to do any adjustments.

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

Labels

Metadata Routing all issues related to metadata routing, slep006, sample props No Changelog Needed size/M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants