Skip to content

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

Merged
glemaitre merged 19 commits into
scikit-learn:mainfrom
OmarManzoor:linear_model_cv_routing
Oct 10, 2023
Merged

FEA Add metadata routing to models that inherit from LinearModelCV#27478
glemaitre merged 19 commits into
scikit-learn:mainfrom
OmarManzoor:linear_model_cv_routing

Conversation

@OmarManzoor

Copy link
Copy Markdown
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

github-actions Bot commented Sep 27, 2023

Copy link
Copy Markdown

✔️ Linting Passed

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

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

Comment thread sklearn/linear_model/_coordinate_descent.py Outdated
@OmarManzoor

Copy link
Copy Markdown
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.

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

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.

Comment thread sklearn/linear_model/_coordinate_descent.py
Comment thread sklearn/linear_model/_coordinate_descent.py Outdated
Comment thread sklearn/linear_model/_coordinate_descent.py Outdated
Comment thread sklearn/linear_model/_coordinate_descent.py Outdated
@adrinjalali

Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

Thank you for the feedback @adrinjalali

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

Nice!

Comment thread doc/metadata_routing.rst Outdated
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
Copy Markdown
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 ;)

@OmarManzoor OmarManzoor Sep 29, 2023

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
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.

@OmarManzoor OmarManzoor Sep 29, 2023

Copy link
Copy Markdown
Contributor Author

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.

Comment thread sklearn/linear_model/_coordinate_descent.py Outdated
Comment thread sklearn/linear_model/_coordinate_descent.py Outdated
Comment thread sklearn/tests/test_metaestimators_metadata_routing.py Outdated
…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
Comment thread sklearn/linear_model/_coordinate_descent.py Outdated
Comment thread sklearn/utils/validation.py Outdated
Comment thread sklearn/linear_model/_coordinate_descent.py Outdated
Parameters to be passed to the CV splitter.

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

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.

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
Copy Markdown
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
Copy Markdown
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 thread sklearn/linear_model/tests/test_coordinate_descent.py Outdated
Comment thread sklearn/linear_model/tests/test_coordinate_descent.py Outdated
Comment thread sklearn/linear_model/tests/test_coordinate_descent.py Outdated
Comment thread sklearn/linear_model/tests/test_coordinate_descent.py Outdated
Comment thread sklearn/linear_model/tests/test_coordinate_descent.py Outdated
Comment on lines +1553 to +1554
or by using the context manager
``with config_context(enable_metadata_routing=True)``.

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.

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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

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

Awesome, thanks @OmarManzoor

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

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