skpro icon indicating copy to clipboard operation
skpro copied to clipboard

[BUG] component estimator fitted state mutates constructor params in wrapper estimators

Open Rusheel86 opened this issue 8 months ago • 3 comments

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

Rusheel86 avatar Mar 14 '26 16:03 Rusheel86

Hi, I’d like to work on this issue. I’ll start by reproducing the failing test and investigating the estimator mutation described above.

Mahaveerjain-18 avatar Mar 14 '26 16:03 Mahaveerjain-18

The solution would be to ensure that clone is called on the scikit-learn estimators.

fkiraly avatar Mar 16 '26 07:03 fkiraly

@fkiraly can i work on this issue ?

amankum2004 avatar Mar 16 '26 10:03 amankum2004