10000 FIX `TransformedTargetRegressor` warns when `set_output` expects dataframe by StefanieSenger · Pull Request #29401 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

FIX TransformedTargetRegressor warns when set_output expects dataframe #29401

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 5 commits into from
Jul 5, 2024
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
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 @@ -198,6 +198,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