Repository navigation
FIX return self from partial_fit of MultiOutputRegressor and BernoulliRBM - #35350
Conversation
…iRBM Both implementations documented `Returns self` but returned None, so the documented chaining idiom (`est.partial_fit(X, y).predict(X)`) raised AttributeError. MultiOutputClassifier and BernoulliRBM.fit already return the estimator, which made the two outliers hard to spot. Non-regression tests assert the return value and keep a control assertion on the sibling that already behaved correctly (scikit-learngh-35347).
|
Thank you for opening your first pull request to scikit-learn! 🎉 To help get your contribution reviewed, please make sure that:
|
cakedev0
left a comment
There was a problem hiding this comment.
Overall LGTM, but let's be a bit less verbose.
| docstrings state, instead of `None`. This restores the documented chaining | ||
| idiom, so that e.g. `est.partial_fit(X, y).predict(X)` no longer raises | ||
| `AttributeError`. Both implementations now behave like their sibling | ||
| :meth:`multioutput.MultiOutputClassifier.partial_fit` and | ||
| :meth:`neural_network.BernoulliRBM.fit`. | ||
| By :user:`lau0708`. |
There was a problem hiding this comment.
Too much details IMO.
| docstrings state, instead of `None`. This restores the documented chaining | |
| idiom, so that e.g. `est.partial_fit(X, y).predict(X)` no longer raises | |
| `AttributeError`. Both implementations now behave like their sibling | |
| :meth:`multioutput.MultiOutputClassifier.partial_fit` and | |
| :meth:`neural_network.BernoulliRBM.fit`. | |
| By :user:`lau0708`. | |
| docstrings state, instead of `None`. | |
| By :user:`lau0708`. |
There was a problem hiding this comment.
Agreed, trimmed. It is now just the two-line summary of the behaviour change — dropped the chaining / AttributeError elaboration. (f27c9ec)
| assert_array_equal(X, Xdigits) | ||
|
|
||
|
|
||
| def test_partial_fit_returns_self(): |
There was a problem hiding this comment.
Maybe you could just add a small assert in an existing partial fit test instead of a new dedicated test?
There was a problem hiding this comment.
Done — moved the assert rbm.partial_fit(batch) is rbm into the existing test_partial_fit loop and dropped the dedicated test. Checked that the assert still bites: removing return self gives assert None is BernoulliRBM(...). (b17ae24)
| assert not hasattr(MultiOutputRegressor(Lasso), "partial_fit") | ||
|
|
||
|
|
||
| def test_partial_fit_returns_self(): |
There was a problem hiding this comment.
Maybe you could just add a small assert in an existing partial fit test instead of a new dedicated test?
There was a problem hiding this comment.
Same here — the assert now sits in test_multi_target_regression_partial_fit on the second partial_fit call, and test_partial_fit_returns_self is gone. (b17ae24)
|
Thanks for the review — all three points are addressed in b17ae24. Heads-up: head moved from 4416287, so the diff you looked at has shifted.
Re-checked that the assertions are load-bearing: with the two production changes reverted, they fail with |
virchan
left a comment
There was a problem hiding this comment.
LGTM as well. Thanks, everyone!
Merging.
Reference Issues/PRs
Fixes #35347
What does this implement/fix? Explain your changes.
partial_fitis documented to returnselffor both of these estimators, and neither did. In both cases the override dropped thereturnthat the implementation it mirrors already has, so the documented chaining idiomest.partial_fit(X, y).predict(X)fails withAttributeError: 'NoneType' object has no attribute 'predict'.MultiOutputRegressor.partial_fit(sklearn/multioutput.py:430) callssuper().partial_fit(...)and discards the result. Its siblingMultiOutputClassifierinherits_MultiOutputEstimator.partial_fit(sklearn/multioutput.py:203), which doesreturn self— the two siblings in the same hierarchy disagree, and only the classifier gets it right.BernoulliRBM.partial_fit(sklearn/neural_network/_rbm.py:310) ends withself._fit(...)and falls off the end of the function, whileBernoulliRBM.fit(sklearn/neural_network/_rbm.py:437) ends withreturn self.Both docstrings have a
Returnssection promising a fitted instance, so this makes the code match the documented contract. Production diff is 2 lines; the rest is one non-regression test per estimator.Deliberately out of scope:
partial_fitof other estimators — I scanned all 24partial_fitdefinitions in the code base against their documentedReturnssection and these are the only two that disagree.AttributeErrorvsNotFittedErrorquestion for chaining on an unfitted estimator — unrelated.Testing
Red on clean
main@ 6e37063 (source build, macOS, Python 3.13.12), the two new tests fail withassert None is <MultiOutputRegressor instance>andassert None is <BernoulliRBM instance>; the same script prints:Green on the branch. Both new tests keep a control assertion that passes before and after the change (
MultiOutputClassifier.partial_fit(...) is clfandBernoulliRBM.fit(...) is rbm), so the red is attributable to the missing return rather than to the test scaffolding.pytest sklearn/tests/test_multioutput.py sklearn/neural_network/tests/test_rbm.pypytest sklearn/tests/test_common.py -k "BernoulliRBM or MultiOutputRegressor or MultiOutputClassifier"ruff check+ruff format --check(0.12.2, as pinned in.pre-commit-config.yaml)Changelog fragment added under
many-modules, since the fix touches bothsklearn.multioutputandsklearn.neural_network; happy to split it per module or move it if you prefer.Introduce yourself
I use scikit-learn mainly for tabular modeling. I was reading the
partial_fitdocstrings while working with incremental training and noticed that theReturnssection promised an instance that two implementations never returned; the asymmetry withMultiOutputClassifier/BernoulliRBM.fitlooked like an oversight rather than a deliberate choice.AI usage disclosure
I used AI assistance for:
Everything reported above (red/green, control assertions, test counts, lint) I ran and checked locally myself.
Any other comments?
The issue was triaged after the earlier attempt (#35349) had already been auto-closed by the bot for being opened while the issue still carried
Needs Triage; this PR is opened after triage, as the contributing guide asks.