Skip to content

Commit 7c91462

Browse files
rprkhglemaitre
andauthored
ENH Raise NotFittedError in get_feature_names_out for Imputers and Isotonic Regression (#25367)
Co-authored-by: Guillaume Lemaitre <g.lemaitre58@gmail.com>
1 parent a18a1ed commit 7c91462

File tree

6 files changed

+11
-11
lines changed

6 files changed

+11
-11
lines changed

doc/whats_new/v1.3.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ Changes impacting all modules
4141
raises a `NotFittedError` if the instance is not fitted. This ensures the error is
4242
consistent in all estimators with the `get_feature_names_out` method.
4343

44-
- :class:`ensemble.StackingClassifier`
45-
- :class:`ensemble.StackingRegressor`
46-
- :class:`ensemble.VotingClassifier`
47-
- :class:`ensemble.VotingRegressor`
4844
- :class:`feature_extraction.text.TfidfTransformer`
4945
- :class:`kernel_approximation.AdditiveChi2Sampler`
46+
- :class:`impute.IterativeImputer`
47+
- :class:`impute.KNNImputer`
48+
- :class:`impute.SimpleImputer`
49+
- :class:`isotonic.IsotonicRegression`
5050
- :class:`preprocessing.Binarizer`
5151
- :class:`preprocessing.MaxAbsScaler`
5252
- :class:`preprocessing.MinMaxScaler`
@@ -60,8 +60,8 @@ Changes impacting all modules
6060
The `NotFittedError` displays an informative message asking to fit the instance
6161
with the appropriate arguments.
6262

63-
:pr:`25294` by :user:`John Pangas <jpangas>` and :pr:`25291`, :pr:`25324`
64-
by :user:`Rahil Parikh <rprkh>`.
63+
:pr:`25294` by :user:`John Pangas <jpangas>` and :pr:`25291`, :pr:`25367` by
64+
:user:`Rahil Parikh <rprkh>`.
6565

6666
Changelog
6767
---------

sklearn/impute/_base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,7 @@ def get_feature_names_out(self, input_features=None):
709709
feature_names_out : ndarray of str objects
710710
Transformed feature names.
711711
"""
712+
check_is_fitted(self, "n_features_in_")
712713
input_features = _check_feature_names_in(self, input_features)
713714
non_missing_mask = np.logical_not(_get_mask(self.statistics_, np.nan))
714715
names = input_features[non_missing_mask]

sklearn/impute/_iterative.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,7 @@ def get_feature_names_out(self, input_features=None):
897897
feature_names_out : ndarray of str objects
898898
Transformed feature names.
899899
"""
900+
check_is_fitted(self, "n_features_in_")
900901
input_features = _check_feature_names_in(self, input_features)
901902
names = self.initial_imputer_.get_feature_names_out(input_features)
902903
return self._concatenate_indicator_feature_names_out(names, input_features)

sklearn/impute/_knn.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ def get_feature_names_out(self, input_features=None):
386386
feature_names_out : ndarray of str objects
387387
Transformed feature names.
388388
"""
389+
check_is_fitted(self, "n_features_in_")
389390
input_features = _check_feature_names_in(self, input_features)
390391
names = input_features[self._valid_mask]
391392
return self._concatenate_indicator_feature_names_out(names, input_features)

sklearn/isotonic.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from .base import BaseEstimator, TransformerMixin, RegressorMixin
1414
from .utils import check_array, check_consistent_length
15-
from .utils.validation import _check_sample_weight
15+
from .utils.validation import _check_sample_weight, check_is_fitted
1616
from .utils._param_validation import Interval, StrOptions
1717
from ._isotonic import _inplace_contiguous_isotonic_regression, _make_unique
1818

@@ -429,6 +429,7 @@ def get_feature_names_out(self, input_features=None):
429429
feature_names_out : ndarray of str objects
430430
An ndarray with one string i.e. ["isotonicregression0"].
431431
"""
432+
check_is_fitted(self, "f_")
432433
class_name = self.__class__.__name__.lower()
433434
return np.asarray([f"{class_name}0"], dtype=object)
434435

sklearn/tests/test_common.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -466,10 +466,7 @@ def test_transformers_get_feature_names_out(transformer):
466466
"DictVectorizer",
467467
"GaussianRandomProjection",
468468
"GenericUnivariateSelect",
469-
"IterativeImputer",
470-
"IsotonicRegression",
471469
"KBinsDiscretizer",
472-
"KNNImputer",
473470
"MissingIndicator",
474471
"RFE",
475472
"RFECV",
@@ -480,7 +477,6 @@ def test_transformers_get_feature_names_out(transformer):
480477
"SelectKBest",
481478
"SelectPercentile",
482479
"SequentialFeatureSelector",
483-
"SimpleImputer",
484480
"SparseRandomProjection",
485481
"SplineTransformer",
486482
"VarianceThreshold",

0 commit comments

Comments
 (0)