[BUG] component estimator fitted state mutates constructor params in wrapper estimators
Describe the bug
test_non_state_changing_method_contract (added in PR #921) reveals that in
wrapper/compositor estimators, sklearn sub-estimators stored as constructor
parameters are fitted in-place during predict calls. Their fitted state then
bleeds into get_params(deep=True), mutating what should be immutable
constructor parameters.
Affected estimators:
- BaggingRegressor
- BootstrapRegressor
- ConditionUncensored
- DeltaPointRegressor
- EnbpiRegressor
- FitUncensored
- GridSearchCV
- HistBinnedProbaRegressor
- MultipleQuantileRegressor
- OnlineDontRefit
- OnlineRefit
- OnlineRefitEveryN
- Pipeline
- RandomizedSearchCV
- ResidualDouble
- SklearnProbaReg
- TransformedTargetRegressor
Root cause: Object Identity Mutation (Re-instantiation)
Diagnostic investigation (documented in PR #921) reveals that these estimators are not necessarily fitting sub-estimators "in-place" (i.e., adding coef_ or intercept_ during predict). Instead, they are re-instantiating or cloning internal component estimators during the predict pass.
While the parameter values remain identical, the object identity changes. This violates the non-state-changing contract because get_params(deep=True) returns a different object instance after a prediction call than it did before.
Using ResidualDouble(LinearRegression()) as a test case:
-
Fitted State: hasattr(params_after['estimator'], 'coef_') is False. (The math is stable).
-
Identity: id(params_before['estimator']) == id(params_after['estimator']) is False. (The pointer has moved).
The failure is caused by the parent estimator overwriting its internal attributes during a supposedly "read-only" operation.
To Reproduce
python -m pytest skpro/regression/tests/test_all_regressors.py::TestAllRegressors::test_non_state_changing_method_contract -v
Expected behavior
Constructor parameters must remain immutable in both value and identity after predict calls. get_params(deep=True) should return identical object references before and after any predict method to ensure the estimator state is strictly non-mutating.
Environment
- OS: Windows
- Python: 3.11
- skpro: current main branch
Hi, I’d like to work on this issue. I’ll start by reproducing the failing test and investigating the estimator mutation described above.
The solution would be to ensure that clone is called on the scikit-learn estimators.
@fkiraly can i work on this issue ?