Skip to content
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

Merged

Conversation

thomasjpfan
Copy link
Member

@thomasjpfan thomasjpfan commented May 26, 2020

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.

@thomasjpfan thomasjpfan changed the title BUG Fixes stacker compatibility with estimators with n_features_in_ BUG Fixes stacker compatibility with estimators without n_features_in_ May 26, 2020

- |Fix| Fixes :class:`ensemble.StackingClassifier` and
:class:`ensemble.StackingRegressor` compatibility with estimators that
does not define `n_features_in_`. :pr:`17357` by `Thomas Fan`_.
Copy link
Member

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

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_

Copy link
Contributor

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)
Copy link
Member

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

@adrinjalali adrinjalali added this to the 0.23.2 milestone Jun 25, 2020
(make_classification, StackingClassifier, LogisticRegression),
(make_regression, StackingRegressor, LinearRegression)
])
def test_stacking_without_n_features_in(make_dataset, Stacking, Regression):
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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):
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
class MyLR(Regression):
class MyEstimator(Estimator):

# for #17353

class MyLR(Regression):
"""Regresion without n_features_in_"""
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"""Regresion without n_features_in_"""
"""Estimator without n_features_in_"""

@glemaitre glemaitre merged commit d1f0d19 into scikit-learn:master Jul 9, 2020
7 checks passed
@glemaitre
Copy link
Contributor

Thanks @thomasjpfan

glemaitre pushed a commit to glemaitre/scikit-learn that referenced this pull request Aug 3, 2020
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.

n_features_in_ in StackingRegressor
4 participants