diff --git a/doc/whats_new/v1.1.rst b/doc/whats_new/v1.1.rst index c0f705a8ceef7..aa1a71466fce0 100644 --- a/doc/whats_new/v1.1.rst +++ b/doc/whats_new/v1.1.rst @@ -314,6 +314,12 @@ Changelog `max_iter` and `tol`. :pr:`21341` by :user:`Arturo Amor `. +:mod:`sklearn.isotonic` +....................... + +- |Enhancement| Adds :term:`get_feature_names_out` to `IsotonicRegression`. + :pr:`22249` by `Thomas Fan`_. + :mod:`sklearn.linear_model` ........................... diff --git a/sklearn/isotonic.py b/sklearn/isotonic.py index 4b1687dea9605..ef8fb3f332e71 100644 --- a/sklearn/isotonic.py +++ b/sklearn/isotonic.py @@ -416,6 +416,26 @@ def predict(self, T): """ return self.transform(T) + # We implement get_feature_names_out here instead of using + # `_ClassNamePrefixFeaturesOutMixin`` because `input_features` are ignored. + # `input_features` are ignored because `IsotonicRegression` accepts 1d + # arrays and the semantics of `feature_names_in_` are not clear for 1d arrays. + def get_feature_names_out(self, input_features=None): + """Get output feature names for transformation. + + Parameters + ---------- + input_features : array-like of str or None, default=None + Ignored. + + Returns + ------- + feature_names_out : ndarray of str objects + An ndarray with one string i.e. ["isotonicregression0"]. + """ + class_name = self.__class__.__name__.lower() + return np.asarray([f"{class_name}0"], dtype=object) + def __getstate__(self): """Pickle-protocol - return state of the estimator.""" state = super().__getstate__() diff --git a/sklearn/tests/test_common.py b/sklearn/tests/test_common.py index a8178a4219485..49ee681316554 100644 --- a/sklearn/tests/test_common.py +++ b/sklearn/tests/test_common.py @@ -382,7 +382,6 @@ def test_pandas_column_name_consistency(estimator): GET_FEATURES_OUT_MODULES_TO_IGNORE = [ "cluster", "ensemble", - "isotonic", "kernel_approximation", "preprocessing", "manifold", diff --git a/sklearn/tests/test_isotonic.py b/sklearn/tests/test_isotonic.py index 4c8ea220e75c0..25f9e26d70d34 100644 --- a/sklearn/tests/test_isotonic.py +++ b/sklearn/tests/test_isotonic.py @@ -695,3 +695,18 @@ def test_isotonic_regression_sample_weight_not_overwritten(): IsotonicRegression().fit(X, y, sample_weight=sample_weight_fit) assert_allclose(sample_weight_fit, sample_weight_original) + + +@pytest.mark.parametrize("shape", ["1d", "2d"]) +def test_get_feature_names_out(shape): + """Check `get_feature_names_out` for `IsotonicRegression`.""" + X = np.arange(10) + if shape == "2d": + X = X.reshape(-1, 1) + y = np.arange(10) + + iso = IsotonicRegression().fit(X, y) + names = iso.get_feature_names_out() + assert isinstance(names, np.ndarray) + assert names.dtype == object + assert_array_equal(["isotonicregression0"], names)