mlforecast/weight-col-on-pipeline
| Pack | Severity | Fix | Versions |
|---|---|---|---|
| MLForecast | error | none, edit by hand | All versions |
MLForecast.fit(..., weight_col=...) and MLForecast.cross_validation(..., weight_col=...) pass sample_weight directly to every estimator's fit method. A scikit-learn Pipeline raises ValueError: Pipeline.fit does not accept the sample_weight parameter unless metadata routing is enabled and every step declares its request.
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)),])mlf = MLForecast(models={"pipe": make_pipeline(StandardScaler(), Ridge())}, freq="D", lags=[1])mlf = MLForecast(models={"pipe": pipe}, freq="D", lags=[1])mlf.fit(train, weight_col="sample_w")What the rule reports
Section titled “What the rule reports”Calls to fit(..., weight_col=...) or cross_validation(..., weight_col=...) on an MLForecast instance configured with a scikit-learn Pipeline or make_pipeline in a module that does not enable enable_metadata_routing.
How to fix it
Section titled “How to fix it”Enable scikit-learn metadata routing with sklearn.set_config(enable_metadata_routing=True) (or sklearn.config_context(enable_metadata_routing=True)) and call .set_fit_request(sample_weight=True) or .set_fit_request(sample_weight=False) on every pipeline step that accepts sample_weight.
Diagnostic message
Section titled “Diagnostic message”MLForecast passes weight_col as fit(..., sample_weight=...), which raises ValueError on a scikit-learn Pipeline unless metadata routing is enabled and each step declares set_fit_request(sample_weight=...).See all rules in the MLForecast pack.