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
BUG Fixes stacker compatibility with estimators without n_features_in_ #17357
BUG Fixes stacker compatibility with estimators without n_features_in_ #17357
Conversation
doc/whats_new/v0.23.rst
Outdated
|
||
- |Fix| Fixes :class:`ensemble.StackingClassifier` and | ||
:class:`ensemble.StackingRegressor` compatibility with estimators that | ||
does not define `n_features_in_`. :pr:`17357` by `Thomas Fan`_. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do
@@ -197,6 +197,15 @@ def fit(self, X, y, sample_weight=None): | |||
|
|||
return self | |||
|
|||
@property |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could be worth using the same structure as others meta estimators for consistency
def n_features_in_(self):
# For consistency with other estimators we raise a AttributeError so
# that hasattr() fails if the estimator isn't fitted.
try:
check_is_fitted(self)
except NotFittedError as nfe:
raise AttributeError(
"{} object has no n_features_in_ attribute."
.format(self.__class__.__name__)
) from nfe
return self.estimators_[0].n_features_in_
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not for this PR but thinking about it, would it make sense to have something shared in a base class (or mixin).
stack = Stacking(estimators=[('lr', MyLR())]) | ||
|
||
# Does not raise | ||
stack.fit(X, y) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe assert an AttributeError when it's accessed after git
(make_classification, StackingClassifier, LogisticRegression), | ||
(make_regression, StackingRegressor, LinearRegression) | ||
]) | ||
def test_stacking_without_n_features_in(make_dataset, Stacking, Regression): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def test_stacking_without_n_features_in(make_dataset, Stacking, Regression): | |
def test_stacking_without_n_features_in(make_dataset, Stacking, Estimator): |
# Stacking supports estimators without `n_features_in_`. Regression test | ||
# for #17353 | ||
|
||
class MyLR(Regression): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
class MyLR(Regression): | |
class MyEstimator(Estimator): |
# for #17353 | ||
|
||
class MyLR(Regression): | ||
"""Regresion without n_features_in_""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"""Regresion without n_features_in_""" | |
"""Estimator without n_features_in_""" |
Thanks @thomasjpfan |
Reference Issues/PRs
Fixes #17353
What does this implement/fix? Explain your changes.
Makes
n_features_in_
a property that depends on the first estimator.