Skip to content

FIX ColumnTransformer metadata routing work within another meta-estimator #28188

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
Feb 1, 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
16 changes: 13 additions & 3 deletions doc/whats_new/v1.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ Version 1.4.1

**In Development**

Metadata Routing
----------------

- |FIX| Fix routing issue with :class:`~compose.ColumnTransformer` when used
inside another meta-estimator.
:pr:`28188` by `Adrin Jalali`_.

DataFrame Support
-----------------

- |Enhancement| Pandas and Polars dataframe are validated directly without ducktyping
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this part of this PR?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, was under the wrong section, but I had forgotten to remove the duplicate. Fixed now.

checks. :pr:`28195` by `Thomas Fan`_.

Changes impacting all modules
-----------------------------

Expand Down Expand Up @@ -70,9 +83,6 @@ Changelog
larger than the number of non-duplicate samples.
:pr:`28165` by :user:`Jérémie du Boisberranger <jeremiedbb>`.

- |Enhancement| Pandas and Polars dataframe are validated directly without ducktyping
checks. :pr:`28195` by `Thomas Fan`_.

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

Expand Down
12 changes: 6 additions & 6 deletions sklearn/compose/_column_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1204,12 +1204,12 @@ def get_metadata_routing(self):
routing information.
"""
router = MetadataRouter(owner=self.__class__.__name__)
for name, step, _, _ in self._iter(
fitted=False,
column_as_labels=False,
skip_drop=True,
skip_empty_columns=True,
):
# Here we don't care about which columns are used for which
# transformers, and whether or not a transformer is used at all, which
# might happen if no columns are selected for that transformer. We
# request all metadata requested by all transformers.
transformers = chain(self.transformers, [("remainder", self.remainder, None)])
for name, step, _ in transformers:
method_mapping = MethodMapping()
if hasattr(step, "fit_transform"):
(
Expand Down
41 changes: 41 additions & 0 deletions sklearn/compose/tests/test_column_transformer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Test the ColumnTransformer.
"""

import pickle
import re
import warnings
Expand Down Expand Up @@ -2526,5 +2527,45 @@ def test_metadata_routing_error_for_column_transformer(method):
getattr(trs, method)(X, y, sample_weight=sample_weight, metadata=metadata)


@pytest.mark.usefixtures("enable_slep006")
def test_get_metadata_routing_works_without_fit():
# Regression test for https://github.com/scikit-learn/scikit-learn/issues/28186
# Make sure ct.get_metadata_routing() works w/o having called fit.
ct = ColumnTransformer([("trans", ConsumingTransformer(), [0])])
ct.get_metadata_routing()


@pytest.mark.usefixtures("enable_slep006")
def test_remainder_request_always_present():
# Test that remainder request is always present.
ct = ColumnTransformer(
[("trans", StandardScaler(), [0])],
remainder=ConsumingTransformer()
.set_fit_request(metadata=True)
.set_transform_request(metadata=True),
)
router = ct.get_metadata_routing()
assert router.consumes("fit", ["metadata"]) == set(["metadata"])


@pytest.mark.usefixtures("enable_slep006")
def test_unused_transformer_request_present():
# Test that the request of a transformer is always present even when not
# used due to no selected columns.
ct = ColumnTransformer(
[
(
"trans",
ConsumingTransformer()
.set_fit_request(metadata=True)
.set_transform_request(metadata=True),
lambda X: [],
)
]
)
router = ct.get_metadata_routing()
assert router.consumes("fit", ["metadata"]) == set(["metadata"])


# End of Metadata Routing Tests
# =============================