Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion sklearn/ensemble/_forest.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ def _validate_X_predict(self, X):
"""
Validate X whenever one tries to predict, apply, predict_proba."""
check_is_fitted(self)

self._check_feature_names(X, reset=False)
return self.estimators_[0]._validate_X_predict(X, check_input=True)

@property
Expand Down Expand Up @@ -1265,6 +1265,11 @@ class labels (multi-output problem).

.. versionadded:: 0.24

feature_names_in_ : ndarray of shape (`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
feature_names_in_ : ndarray of shape (`n_features_in_`,)
feature_names_in_ : ndarray of shape (n_features_in_,)

We also do X : array-like of shape (n_samples, n_features) without ticks.

Names of features seen during :term:`fit`. Defined only when `X`
has feature names that are all strings.
.. versionadded:: 1.0

n_outputs_ : int
The number of outputs when ``fit`` is performed.

Expand Down Expand Up @@ -1590,6 +1595,11 @@ class RandomForestRegressor(ForestRegressor):

.. versionadded:: 0.24

feature_names_in_ : ndarray of shape (`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
feature_names_in_ : ndarray of shape (`n_features_in_`,)
feature_names_in_ : ndarray of shape (n_features_in_,)

Copy link
Member Author

Choose a reason for hiding this comment

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

As explained in another PR, this would break sphinx rendering because it would treat those as a link to a reference.

Names of features seen during :term:`fit`. Defined only when `X`
has feature names that are all strings.
.. versionadded:: 1.0

n_outputs_ : int
The number of outputs when ``fit`` is performed.

Expand Down Expand Up @@ -1920,6 +1930,11 @@ class labels (multi-output problem).

.. versionadded:: 0.24

feature_names_in_ : ndarray of shape (`n_features_in_`,)
Names of features seen during :term:`fit`. Defined only when `X`
has feature names that are all strings.
.. versionadded:: 1.0

n_outputs_ : int
The number of outputs when ``fit`` is performed.

Expand Down Expand Up @@ -2222,6 +2237,11 @@ class ExtraTreesRegressor(ForestRegressor):

.. versionadded:: 0.24

feature_names_in_ : ndarray of shape (`n_features_in_`,)
Names of features seen during :term:`fit`. Defined only when `X`
has feature names that are all strings.
.. versionadded:: 1.0

n_outputs_ : int
The number of outputs.

Expand Down Expand Up @@ -2454,6 +2474,11 @@ class RandomTreesEmbedding(BaseForest):

.. versionadded:: 0.24

feature_names_in_ : ndarray of shape (`n_features_in_`,)
Names of features seen during :term:`fit`. Defined only when `X`
has feature names that are all strings.
.. versionadded:: 1.0

n_outputs_ : int
The number of outputs when ``fit`` is performed.

Expand Down
15 changes: 14 additions & 1 deletion sklearn/ensemble/_stacking.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,11 @@ def fit(self, X, y, sample_weight=None):
est_fitted_idx = 0
for name_est, org_est in zip(names, all_estimators):
if org_est != "drop":
self.named_estimators_[name_est] = self.estimators_[est_fitted_idx]
current_estimator = self.estimators_[est_fitted_idx]
self.named_estimators_[name_est] = current_estimator
est_fitted_idx += 1
if hasattr(current_estimator, "feature_names_in_"):
self.feature_names_in_ = current_estimator.feature_names_in_
else:
self.named_estimators_[name_est] = "drop"

Expand Down Expand Up @@ -373,6 +376,11 @@ class StackingClassifier(ClassifierMixin, _BaseStacking):

.. versionadded:: 0.24

feature_names_in_ : ndarray of shape (`n_features_in_`,)
Names of features seen during :term:`fit`. Only defined if the
underlying estimators expose such an attribute when fit.
.. versionadded:: 1.0

final_estimator_ : estimator
The classifier which predicts given the output of `estimators_`.

Expand Down Expand Up @@ -649,6 +657,11 @@ class StackingRegressor(RegressorMixin, _BaseStacking):

.. versionadded:: 0.24

feature_names_in_ : ndarray of shape (`n_features_in_`,)
Names of features seen during :term:`fit`. Only defined if the
underlying estimators expose such an attribute when fit.
.. versionadded:: 1.0

final_estimator_ : estimator
The regressor to stacked the base estimators fitted.

Expand Down
13 changes: 13 additions & 0 deletions sklearn/ensemble/_voting.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ def fit(self, X, y, sample_weight=None):
current_est = est if est == "drop" else next(est_iter)
self.named_estimators_[name] = current_est

if hasattr(current_est, "feature_names_in_"):
self.feature_names_in_ = current_est.feature_names_in_

return self

def fit_transform(self, X, y=None, **fit_params):
Expand Down Expand Up @@ -217,6 +220,11 @@ class VotingClassifier(ClassifierMixin, _BaseVoting):

.. versionadded:: 0.24

feature_names_in_ : ndarray of shape (`n_features_in_`,)
Names of features seen during :term:`fit`. Only defined if the
underlying estimators expose such an attribute when fit.
.. versionadded:: 1.0

See Also
--------
VotingRegressor : Prediction voting regressor.
Expand Down Expand Up @@ -466,6 +474,11 @@ class VotingRegressor(RegressorMixin, _BaseVoting):

.. versionadded:: 0.24

feature_names_in_ : ndarray of shape (`n_features_in_`,)
Names of features seen during :term:`fit`. Only defined if the
underlying estimators expose such an attribute when fit.
.. versionadded:: 1.0

See Also
--------
VotingClassifier : Soft Voting/Majority Rule classifier.
Expand Down
1 change: 0 additions & 1 deletion sklearn/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,6 @@ def test_check_n_features_in_after_fitting(estimator):

COLUMN_NAME_MODULES_TO_IGNORE = {
"compose",
"ensemble",
"feature_extraction",
"kernel_approximation",
"model_selection",
Expand Down