Skip to content

BUG Fixes stacker compatibility with estimators without n_features_in_ - #17357

Merged
glemaitre merged 8 commits into
scikit-learn:masterfrom
thomasjpfan:stacking_delagate_n_features_in
Jul 9, 2020
Merged

glemaitre merged 8 commits into
scikit-learn:masterfrom
thomasjpfan:stacking_delagate_n_features_in

Conversation

@thomasjpfan

@thomasjpfan thomasjpfan commented May 26, 2020 •

Copy link
Copy Markdown
Member

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
Comment thread 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`_.

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.

do


return self

@property

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.

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

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).

Comment thread sklearn/ensemble/tests/test_stacking.py Outdated
stack = Stacking(estimators=[('lr', MyLR())])

# Does not raise
stack.fit(X, y)

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.

maybe assert an AttributeError when it's accessed after git

@adrinjalali adrinjalali added this to the 0.23.2 milestone Jun 25, 2020
Comment thread sklearn/ensemble/tests/test_stacking.py Outdated
(make_classification, StackingClassifier, LogisticRegression),
(make_regression, StackingRegressor, LinearRegression)
])
def test_stacking_without_n_features_in(make_dataset, Stacking, Regression):

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.

Suggested change
def test_stacking_without_n_features_in(make_dataset, Stacking, Regression):
def test_stacking_without_n_features_in(make_dataset, Stacking, Estimator):

Comment thread sklearn/ensemble/tests/test_stacking.py Outdated
# Stacking supports estimators without `n_features_in_`. Regression test
# for #17353

class MyLR(Regression):

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.

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

Comment thread sklearn/ensemble/tests/test_stacking.py Outdated
# for #17353

class MyLR(Regression):
"""Regresion without n_features_in_"""

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.

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

@glemaitre
glemaitre merged commit d1f0d19 into scikit-learn:master Jul 9, 2020
@glemaitre

Copy link
Copy Markdown
Member

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