Skip to content

FIX fix available_if for MultiOutputRegressor.partial_fit #26333

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
May 5, 2023
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
8 changes: 8 additions & 0 deletions doc/whats_new/v1.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,14 @@ Changelog
`return_indices` to return the train-test indices of each cv split.
:pr:`25659` by :user:`Guillaume Lemaitre <glemaitre>`.

:mod:`sklearn.multioutput`
..........................

- |Fix| :func:`getattr` on :meth:`multioutput.MultiOutputRegressor.partial_fit`
and :meth:`multioutput.MultiOutputClassifier.partial_fit` now correctly raise
an `AttributeError` if done before calling `fit`. :pr:`26333` by `Adrin
Jalali`_.

:mod:`sklearn.naive_bayes`
..........................

Expand Down
12 changes: 8 additions & 4 deletions sklearn/multioutput.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,19 @@ def _partial_fit_estimator(


def _available_if_estimator_has(attr):
"""Return a function to check if `estimator` or `estimators_` has `attr`.
"""Return a function to check if the sub-estimator(s) has(have) `attr`.

Helper for Chain implementations.
"""

def _check(self):
return hasattr(self.estimator, attr) or all(
hasattr(est, attr) for est in self.estimators_
)
if hasattr(self, "estimators_"):
return all(hasattr(est, attr) for est in self.estimators_)

if hasattr(self.estimator, attr):
return True

return False

return available_if(_check)

Expand Down
9 changes: 9 additions & 0 deletions sklearn/tests/test_multioutput.py
Original file line number Diff line number Diff line change
Expand Up @@ -766,3 +766,12 @@ def test_multioutputregressor_ducktypes_fitted_estimator():

# Does not raise
reg.predict(X)


def test_multioutput_regressor_has_partial_fit():
# Test that an unfitted MultiOutputRegressor handles available_if for
# partial_fit correctly
est = MultiOutputRegressor(LinearRegression())
msg = "This 'MultiOutputRegressor' has no attribute 'partial_fit'"
with pytest.raises(AttributeError, match=msg):
getattr(est, "partial_fit")