sklearn/transformed-target-pipeline-weight
| Pack | Severity | Fix | Versions |
|---|---|---|---|
| scikit-learn | error | none, edit by hand | All versions |
TransformedTargetRegressor.fit(X, y, sample_weight=...) passes sample_weight straight to self.regressor.fit. When regressor is a Pipeline, Pipeline.fit raises ValueError: Pipeline.fit does not accept the sample_weight parameter unless metadata routing is enabled.
import sklearn
sklearn.set_config(enable_metadata_routing=True)pipe = Pipeline([ ("scale", StandardScaler().set_fit_request(sample_weight=False)), ("model", Ridge().set_fit_request(sample_weight=True)),])ttr = TransformedTargetRegressor(regressor=make_pipeline(StandardScaler(), Ridge()), func=np.log1p, inverse_func=np.expm1)ttr = TransformedTargetRegressor(regressor=pipe, func=np.log1p, inverse_func=np.expm1)ttr.fit(X, y, sample_weight=weights)What the rule reports
Section titled “What the rule reports”Calls to fit(..., sample_weight=...) on a TransformedTargetRegressor whose inner regressor is a Pipeline or make_pipeline in a module without enable_metadata_routing=True.
How to fix it
Section titled “How to fix it”Enable metadata routing with sklearn.set_config(enable_metadata_routing=True) (or sklearn.config_context(enable_metadata_routing=True)) and declare .set_fit_request(sample_weight=True/False) on every pipeline step that accepts sample_weight, or pass stepname__sample_weight without metadata routing.
Diagnostic message
Section titled “Diagnostic message”TransformedTargetRegressor forwards sample_weight directly to its inner regressor, which raises ValueError on a Pipeline unless metadata routing is enabled and each step declares set_fit_request(sample_weight=...).See all rules in the scikit-learn pack.