Skip to content

ENH Raise NotFittedError in get_feature_names_out for Imputers and Isotonic Regression #25367

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

Merged
merged 9 commits into from
Jan 18, 2023
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
12 changes: 6 additions & 6 deletions doc/whats_new/v1.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ Changes impacting all modules
raises a `NotFittedError` if the instance is not fitted. This ensures the error is
consistent in all estimators with the `get_feature_names_out` method.

- :class:`ensemble.StackingClassifier`
- :class:`ensemble.StackingRegressor`
- :class:`ensemble.VotingClassifier`
- :class:`ensemble.VotingRegressor`
- :class:`feature_extraction.text.TfidfTransformer`
Copy link
Member

Choose a reason for hiding this comment

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

I reordered those to be in alphabetic order.

- :class:`kernel_approximation.AdditiveChi2Sampler`
- :class:`impute.IterativeImputer`
- :class:`impute.KNNImputer`
- :class:`impute.SimpleImputer`
- :class:`isotonic.IsotonicRegression`
- :class:`preprocessing.Binarizer`
- :class:`preprocessing.MaxAbsScaler`
- :class:`preprocessing.MinMaxScaler`
Expand All @@ -60,8 +60,8 @@ Changes impacting all modules
The `NotFittedError` displays an informative message asking to fit the instance
with the appropriate arguments.

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

Changelog
---------
Expand Down
1 change: 1 addition & 0 deletions sklearn/impute/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,7 @@ def get_feature_names_out(self, input_features=None):
feature_names_out : ndarray of str objects
Transformed feature names.
"""
check_is_fitted(self, "n_features_in_")
input_features = _check_feature_names_in(self, input_features)
non_missing_mask = np.logical_not(_get_mask(self.statistics_, np.nan))
names = input_features[non_missing_mask]
Expand Down
1 change: 1 addition & 0 deletions sklearn/impute/_iterative.py
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,7 @@ def get_feature_names_out(self, input_features=None):
feature_names_out : ndarray of str objects
Transformed feature names.
"""
check_is_fitted(self, "n_features_in_")
input_features = _check_feature_names_in(self, input_features)
names = self.initial_imputer_.get_feature_names_out(input_features)
return self._concatenate_indicator_feature_names_out(names, input_features)
1 change: 1 addition & 0 deletions sklearn/impute/_knn.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ def get_feature_names_out(self, input_features=None):
feature_names_out : ndarray of str objects
Transformed feature names.
"""
check_is_fitted(self, "n_features_in_")
input_features = _check_feature_names_in(self, input_features)
names = input_features[self._valid_mask]
return self._concatenate_indicator_feature_names_out(names, input_features)
3 changes: 2 additions & 1 deletion sklearn/isotonic.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from .base import BaseEstimator, TransformerMixin, RegressorMixin
from .utils import check_array, check_consistent_length
from .utils.validation import _check_sample_weight
from .utils.validation import _check_sample_weight, check_is_fitted
from .utils._param_validation import Interval, StrOptions
from ._isotonic import _inplace_contiguous_isotonic_regression, _make_unique

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

Expand Down
4 changes: 0 additions & 4 deletions sklearn/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,10 +466,7 @@ def test_transformers_get_feature_names_out(transformer):
"DictVectorizer",
"GaussianRandomProjection",
"GenericUnivariateSelect",
"IterativeImputer",
"IsotonicRegression",
"KBinsDiscretizer",
"KNNImputer",
"MissingIndicator",
"RFE",
"RFECV",
Expand All @@ -480,7 +477,6 @@ def test_transformers_get_feature_names_out(transformer):
"SelectKBest",
"SelectPercentile",
"SequentialFeatureSelector",
"SimpleImputer",
"SparseRandomProjection",
"SplineTransformer",
"VarianceThreshold",
Expand Down