Great question and great answers! (Thanks @Sycorax, @AN6U5, and @user1771485). All of them helped me a lot to find an answer to the specific case, where I needed to use sample_weight during GridSearchCV, but my estimator was obtained using Pipeline. The issue differs from the previous solutions because Pipeline does not support fit_param; indeed, if you try to use fit_param = {... }
during the fit step (of GridSearchCV) you'll get
Pipeline.fit does not accept the fit_param parameter. You can pass parameters to specific steps of your pipeline using the stepname__parameter format, e.g. Pipeline.fit(X, y, logisticregression__sample_weight=sample_weight)
The pipeline I was using was
pipe = Pipeline(steps=[('normalizer', norm), ('estimator', svr)])
where norm
was a normalization step, svr = SVR()
, and the parameter grid
parameters_svr = dict (estimator = [svr], estimator__kernel = ['rbf', 'sigmoid'], ...)
Then, as advised by @user1771485
grid = GridSearchCV (estimator = pipe, param_grid = parameters_svr, cv = 3,
scoring = 'neg_mean_squared_error',
return_train_score = True, refit = True, n_jobs = -1)
and finally, (the part that truly matters)
grid.fit (X,y, estimator__sample_weight= weights)