Skip to content

Commit 270ac2e

Browse files
authored
Check features_names_in_ in sklearn.multioutput (#20847)
1 parent 786c4e5 commit 270ac2e

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

sklearn/multioutput.py

+29
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ def partial_fit(self, X, y, classes=None, sample_weight=None):
145145

146146
if first_time and hasattr(self.estimators_[0], "n_features_in_"):
147147
self.n_features_in_ = self.estimators_[0].n_features_in_
148+
if first_time and hasattr(self.estimators_[0], "feature_names_in_"):
149+
self.feature_names_in_ = self.estimators_[0].feature_names_in_
148150

149151
return self
150152

@@ -206,6 +208,8 @@ def fit(self, X, y, sample_weight=None, **fit_params):
206208

207209
if hasattr(self.estimators_[0], "n_features_in_"):
208210
self.n_features_in_ = self.estimators_[0].n_features_in_
211+
if hasattr(self.estimators_[0], "feature_names_in_"):
212+
self.feature_names_in_ = self.estimators_[0].feature_names_in_
209213

210214
return self
211215

@@ -279,6 +283,12 @@ class MultiOutputRegressor(RegressorMixin, _MultiOutputEstimator):
279283
280284
.. versionadded:: 0.24
281285
286+
feature_names_in_ : ndarray of shape (`n_features_in_`,)
287+
Names of features seen during :term:`fit`. Only defined if the
288+
underlying estimators expose such an attribute when fit.
289+
290+
.. versionadded:: 1.0
291+
282292
Examples
283293
--------
284294
>>> import numpy as np
@@ -362,6 +372,12 @@ class MultiOutputClassifier(ClassifierMixin, _MultiOutputEstimator):
362372
363373
.. versionadded:: 0.24
364374
375+
feature_names_in_ : ndarray of shape (`n_features_in_`,)
376+
Names of features seen during :term:`fit`. Only defined if the
377+
underlying estimators expose such an attribute when fit.
378+
379+
.. versionadded:: 1.0
380+
365381
Examples
366382
--------
367383
>>> import numpy as np
@@ -675,6 +691,12 @@ class labels for each estimator in the chain.
675691
676692
.. versionadded:: 0.24
677693
694+
feature_names_in_ : ndarray of shape (`n_features_in_`,)
695+
Names of features seen during :term:`fit`. Defined only when `X`
696+
has feature names that are all strings.
697+
698+
.. versionadded:: 1.0
699+
678700
Examples
679701
--------
680702
>>> from sklearn.datasets import make_multilabel_classification
@@ -773,6 +795,7 @@ def decision_function(self, X):
773795
Returns the decision function of the sample for each model
774796
in the chain.
775797
"""
798+
X = self._validate_data(X, accept_sparse=True, reset=False)
776799
Y_decision_chain = np.zeros((X.shape[0], len(self.estimators_)))
777800
Y_pred_chain = np.zeros((X.shape[0], len(self.estimators_)))
778801
for chain_idx, estimator in enumerate(self.estimators_):
@@ -860,6 +883,12 @@ class RegressorChain(MetaEstimatorMixin, RegressorMixin, _BaseChain):
860883
861884
.. versionadded:: 0.24
862885
886+
feature_names_in_ : ndarray of shape (`n_features_in_`,)
887+
Names of features seen during :term:`fit`. Defined only when `X`
888+
has feature names that are all strings.
889+
890+
.. versionadded:: 1.0
891+
863892
Examples
864893
--------
865894
>>> from sklearn.multioutput import RegressorChain

sklearn/tests/test_common.py

-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,6 @@ def test_check_n_features_in_after_fitting(estimator):
328328
"feature_extraction",
329329
"kernel_approximation",
330330
"model_selection",
331-
"multioutput",
332331
}
333332

334333
_estimators_to_test = list(

0 commit comments

Comments
 (0)