[go: up one dir, main page]

Skip to content
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

ENH make fit_transform and fit_predict composite methods (SLEP6) #26506

Merged
merged 15 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Address Guillaume's comments
  • Loading branch information
adrinjalali committed Jun 21, 2023
commit 072cbff8b02e75570bd09c6dbdff2a8cf03b4ff3
4 changes: 2 additions & 2 deletions doc/whats_new/v1.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ TODO: update at the time of the release.
:mod:`sklearn.base`
...................

- |Enhancement| :func:`base.ClusterMixin.fit_predict` and
:func:`base.OutlierMixin.fit_predict` now accept ``**kwargs`` which are
- |Enhancement| :meth:`base.ClusterMixin.fit_predict` and
:meth:`base.OutlierMixin.fit_predict` now accept ``**kwargs`` which are
passed to the ``fit`` method of the the estimator. :pr:`26506` by `Adrin
Jalali`_.
17 changes: 14 additions & 3 deletions sklearn/tests/test_metadata_routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,10 @@ def inverse_transform(self, X, sample_weight=None):


def test_composite_methods():
adrinjalali marked this conversation as resolved.
Show resolved Hide resolved
# Test the behavior and the values of methods (composite methods) whose
# request values are a union of requests by other methods (simple methods).
# fit_transform and fit_predict are the only composite methods we have in
# scikit-learn.
class SimpleEstimator(BaseEstimator):
# This class should have every set_{method}_request
def fit(self, X, y, foo=None, bar=None):
Expand All @@ -1033,8 +1037,14 @@ def transform(self, X, other_param=None):
pass # pragma: no cover

est = SimpleEstimator()
est.get_metadata_routing().fit_transform.requests == {}
est.get_metadata_routing().fit_predict.requests == {}
# Since no request is set for fit or predict or transform, the request for
# fit_transform and fit_predict should also be empty.
assert est.get_metadata_routing().fit_transform.requests == {
"bar": None,
"foo": None,
"other_param": None,
}
assert est.get_metadata_routing().fit_predict.requests == {"bar": None, "foo": None}

# setting the request on only one of them should raise an error
est.set_fit_request(foo=True, bar="test")
Expand All @@ -1047,7 +1057,8 @@ def transform(self, X, other_param=None):
with pytest.raises(ValueError, match="Conflicting metadata requests for"):
est.get_metadata_routing().fit_predict

# now the requests are consistent
# now the requests are consistent and getting the requests for fit_predict
# shouldn't raise.
est.set_predict_request(foo=True, bar="test")
est.get_metadata_routing().fit_predict

Expand Down