Skip to content

FEA Add metadata routing to models that inherit from LinearModelCV #27478

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 19 commits into from
Oct 10, 2023

Conversation

OmarManzoor
Copy link
Contributor

Reference Issues/PRs

Towards #22893

What does this implement/fix? Explain your changes.

  • Adds metadata routing to LassoCV, ElasticNetCV, MultiTaskElasticNetCV, MultiTaskLassoCV as all of them inherit from the base LinearModelCV.

Any other comments?

CC: @adrinjalali @glemaitre

@github-actions
Copy link

github-actions bot commented Sep 27, 2023

✔️ Linting Passed

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

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

@OmarManzoor
Copy link
Contributor Author

@adrinjalali @glemaitre So I fixed the tests and made some adjustments. Could you kindly have a look to see if the overall approach and the corresponding additional test seems fine? If yes then we can add comments to refine this PR.

Copy link
Member

@adrinjalali adrinjalali left a comment

Choose a reason for hiding this comment

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

I think we can do routing only for CV and let these classes be consumers of sample_weight, and the underlying fit of the sub-estimators don't accept anything other than sample_weight anyway.

@adrinjalali
Copy link
Member

Also need to move the estimators from unsupported to supported in metadata_routing.rst

@OmarManzoor OmarManzoor marked this pull request as ready for review September 29, 2023 07:51
@OmarManzoor
Copy link
Contributor Author

Thank you for the feedback @adrinjalali

Copy link
Member

@adrinjalali adrinjalali left a comment

Choose a reason for hiding this comment

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

Nice!

Comment on lines 1555 to 1556
if sample_weight is not None and not has_fit_parameter(self, "sample_weight"):
raise ValueError("Underlying estimator does not support sample weights.")
Copy link
Member

Choose a reason for hiding this comment

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

this can't happen, can it? if sample weight is not in the signature, it cannot possibly be passed ;)

Copy link
Contributor Author

@OmarManzoor OmarManzoor Sep 29, 2023

Choose a reason for hiding this comment

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

MultiTaskElasticNetCV and MultiTaskLassoCV do not support sample_weight currently. But now as we have added **params to both, technically someone can pass sample_weight and if they do and it is unchecked, the code might break unexpectedly. What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

the thing is, sample_weight could in theory be used in the cv splitter. So we'd need to check, if the splitter doesn't use it, and the estimator also doesn't support it, then raise.

Copy link
Contributor Author

@OmarManzoor OmarManzoor Sep 29, 2023

Choose a reason for hiding this comment

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

What happens if the splitter uses it? Then if we allow sample weight to be passed further than the splitter, the place where we fit the base/child model will pass sample weight as well and that won't work with these estimators. Do we then need to check this where we are using process routing? That is if the splitter.split does not contain sample weight and the estimator doesn't support it as well, then we raise. If the splitter has it but the estimator doesn't support it we set it to None after process routing.

…th don't support, if cv splitter supports but estimator doesn't after routing set sample weight to be None
@glemaitre glemaitre self-requested a review October 2, 2023 13:48
Parameters to be passed to the CV splitter.

.. versionadded:: 1.4
Only available if `enable_metadata_routing=True`. See
Copy link
Member

Choose a reason for hiding this comment

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

As in the other PR, I would suggest to add a reference to set_config and config_context to be explicit where enable_metadata_routing needs to be set.

Copy link
Member

Choose a reason for hiding this comment

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

This would need to be done in all places.

Copy link
Member

Choose a reason for hiding this comment

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

we can do this in a separate PR.

Comment on lines 1553 to 1554
or by using the context manager
``with config_context(enable_metadata_routing=True)``.
Copy link
Member

Choose a reason for hiding this comment

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

we shouldn't be advertising the context manager really. It'll cause a lot of issues in this case, if it's enabled in certain places of the code and not in other places.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Member

@adrinjalali adrinjalali left a comment

Choose a reason for hiding this comment

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

Awesome, thanks @OmarManzoor

Copy link
Member

@glemaitre glemaitre left a comment

Choose a reason for hiding this comment

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

LGTM as well.

@glemaitre glemaitre merged commit 9226441 into scikit-learn:main Oct 10, 2023
@OmarManzoor OmarManzoor deleted the linear_model_cv_routing branch October 10, 2023 08:18
glemaitre pushed a commit to glemaitre/scikit-learn that referenced this pull request Oct 31, 2023
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.

3 participants