Skip to content

Commit

Permalink
FIX TransformedTargetRegressor warns when set_output expects data…
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanieSenger committed Jul 5, 2024
1 parent d391353 commit 938c36b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
17 changes: 17 additions & 0 deletions doc/whats_new/v1.5.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ For a short description of the main highlights of the release, please refer to

.. include:: changelog_legend.inc

.. _changes_1_5_2:

Version 1.5.2
=============

**release date of 1.5.2**

Changelog
---------

:mod:`sklearn.compose`
......................

- |Fix| Fixed :class:`compose.TransformedTargetRegressor` not to raise `UserWarning` if
transform output is set to `pandas` or `polars`, since it isn't a transformer.
:pr:`29401` by :user:`Stefanie Senger <StefanieSenger>`.

.. _changes_1_5_1:

Version 1.5.1
Expand Down
4 changes: 4 additions & 0 deletions sklearn/compose/_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ def _fit_transformer(self, y):
validate=True,
check_inverse=self.check_inverse,
)
# We are transforming the target here and not the features, so we set the
# output of FunctionTransformer() to be a numpy array (default) and to not
# depend on the global configuration:
self.transformer_.set_output(transform="default")
# XXX: sample_weight is not currently passed to the
# transformer. However, if transformer starts using sample_weight, the
# code should be modified accordingly. At the time to consider the
Expand Down
19 changes: 18 additions & 1 deletion sklearn/compose/tests/test_target.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import warnings

import numpy as np
import pytest

from sklearn import datasets
from sklearn import config_context, datasets
from sklearn.base import BaseEstimator, TransformerMixin, clone
from sklearn.compose import TransformedTargetRegressor
from sklearn.dummy import DummyRegressor
Expand Down Expand Up @@ -393,3 +395,18 @@ def test_transform_target_regressor_pass_extra_predict_parameters():
regr.fit(X, y)
regr.predict(X, check_input=False)
assert regr.regressor_.predict_called


@pytest.mark.parametrize("output_format", ["pandas", "polars"])
def test_transform_target_regressor_not_warns_with_global_output_set(output_format):
"""Test that TransformedTargetRegressor will not raise warnings if
set_config(transform_output="pandas"/"polars") is set globally; regression test for
issue #29361."""
X, y = datasets.make_regression()
y = np.abs(y) + 1
with config_context(transform_output=output_format):
with warnings.catch_warnings():
warnings.simplefilter("error")
TransformedTargetRegressor(
regressor=LinearRegression(), func=np.log, inverse_func=np.exp
).fit(X, y)

0 comments on commit 938c36b

Please sign in to comment.