Skip to content

Delegate choice of final model to sub class in LinearModelCV #17099

Merged
agramfort merged 5 commits intoscikit-learn:masterfrom
mathurinm:design_linearmodelcv
May 6, 2020
Merged

Delegate choice of final model to sub class in LinearModelCV #17099
agramfort merged 5 commits intoscikit-learn:masterfrom
mathurinm:design_linearmodelcv

Conversation

@mathurinm
Copy link
Copy Markdown
Contributor

Reference Issues/PRs

What does this implement/fix? Explain your changes.

The current design of the base class LinearModelCV uses conditions based on l1_ratio to instantiate, in a hardcoded fashion, the model to be fitted after the best alpha has been determined by CV (eg Lasso for LassoCV, MultitaskElasticNet for MultitaskElasticNetCV)

This is not a common design and it makes it difficult to subclass LinearModelCV when implementing AnotherEstimatorCV

With the proposed PR, the model to instantiate is implemented by the sub class, in _get_model(self)

I also implemented _is_multitask(self) to address a similar issue

@agramfort

@NicolasHug do you have an opinion on this ?

Any other comments?

Copy link
Copy Markdown
Member

@NicolasHug NicolasHug left a comment

Choose a reason for hiding this comment

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

Thanks @mathurinm , it does look better

LGTM when green

Comment on lines +1509 to +1510
def _get_model(self):
return Lasso()
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 feel like returning the class rather than an instance is more natural because we are going to use set_params on it later anyway

Suggested change
def _get_model(self):
return Lasso()
def _get_model_class(self):
return Lasso

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.

so model.set_params(**common_params) becomes model = self._get_model_class(**common_params) ?

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.

@NicolasHug Actually we call model.get_params() before we set the parameters, so we need a "blank" instance available before set_params()

@agramfort
Copy link
Copy Markdown
Member

this should greatly facilitate subclassing in other packages. thanks @mathurinm for taking a stab at this !

@mathurinm
Copy link
Copy Markdown
Contributor Author

also pinging @ogrisel who is interested in linear models :)

self.selection = selection

@abstractmethod
def _get_model(self):
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.

To be consistent with scikit-learn conventions, I think _get_estimator would be more appropriate.

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.

If a class is returned, _get_estimator_class (or _get_estimator_type? class is probably better).

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.

I have renamed to _get_estimator(). We need to create a blank instance (to get its parameters) so I kept the design where we return an estimator and not the class.

Copy link
Copy Markdown
Member

@ogrisel ogrisel 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. Few nitpicks / small suggestions:

Copy link
Copy Markdown
Member

@ogrisel ogrisel left a comment

Choose a reason for hiding this comment

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

I meant to approve instead of comment in the previous review.

@cmarmo
Copy link
Copy Markdown
Contributor

cmarmo commented May 6, 2020

All checks passed and three approvals: @scikit-learn/core-devs, ready to be merged? Thanks!

@agramfort agramfort merged commit 2a5c22a into scikit-learn:master May 6, 2020
@agramfort
Copy link
Copy Markdown
Member

3 approvals are enough so let's get this one in. thanks @mathurinm

@mathurinm mathurinm deleted the design_linearmodelcv branch May 7, 2020 07:18
gio8tisu pushed a commit to gio8tisu/scikit-learn that referenced this pull request May 15, 2020
…learn#17099)

* init

* flake8 error by vscode autoformatting

* remove returns

* get_model > get_estimator

* cosmit Tom + autoformat on save error again
viclafargue pushed a commit to viclafargue/scikit-learn that referenced this pull request Jun 26, 2020
…learn#17099)

* init

* flake8 error by vscode autoformatting

* remove returns

* get_model > get_estimator

* cosmit Tom + autoformat on save error again
jayzed82 pushed a commit to jayzed82/scikit-learn that referenced this pull request Oct 22, 2020
…learn#17099)

* init

* flake8 error by vscode autoformatting

* remove returns

* get_model > get_estimator

* cosmit Tom + autoformat on save error again
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.

7 participants