Skip to content

sklego/decay-without-sample-weight

Pack Severity Fix Versions
scikit-lego error unsafe, with xpd check --fix --unsafe-fixes All versions

DecayEstimator passes sample_weight to the fit of the model it wraps, so wrapping a Pipeline or a model without sample_weight raises ValueError. Put the DecayEstimator around the last step, inside the pipeline.

model = DecayEstimator(make_pipeline(StandardScaler(), Ridge()))
model = make_pipeline(StandardScaler(), DecayEstimator(Ridge()))

The rule reports DecayEstimator around a Pipeline, around scikit-lego grouped or hierarchical meta estimators (GroupedRegressor, GroupedClassifier, GroupedPredictor, HierarchicalRegressor, HierarchicalClassifier, HierarchicalPredictor), and around a scikit-learn model whose fit takes no sample_weight, such as KNeighborsRegressor, GaussianProcessRegressor and LinearDiscriminantAnalysis. DecayEstimator passes its weights as fit(X, y, sample_weight=...). For grouped or hierarchical models, wrap the inner estimator inside the grouped wrapper, such as GroupedRegressor(DecayEstimator(Ridge()), groups=["hour"]).

xpd check --fix --unsafe-fixes moves DecayEstimator around the last step of a two-step make_pipeline or make_debug_pipeline. Edit other pipeline layouts and unweighted models by hand.

The pipeline raised ValueError "Pipeline.fit does not accept the sample_weight parameter", and KNeighborsRegressor raised TypeError "Model KNeighborsRegressor.fit() does not have 'sample_weight'". With the DecayEstimator around the last step, the pipeline fitted.

The list of models has 22 names, among them PassiveAggressiveClassifier, PassiveAggressiveRegressor, CCA and PLSCanonical. It holds every classifier and regressor of sklearn.utils.all_estimators() on scikit-learn 1.9.1 that takes no argument to build and whose fit raised that TypeError inside a DecayEstimator.

DecayEstimator passes sample_weight to the model's fit, and a Pipeline raises ValueError "Pipeline.fit does not accept the sample_weight parameter". Put the DecayEstimator around the last step, inside the pipeline.

See all rules in the scikit-lego pack.