Search code examples
pythongridsearchcv

How to change the n_jobs parameter of GridSearchCV.best_estimator_


I'm running GridSearchCV where the base estimator is a RandomForestRegressor. I've found that it's much more efficient to split the processors between the estimator and the grid search, so of the machine's 24 available processors, I give n_jobs=4 to the regressor and n_jobs=6 to the grid search.

But once I have my best estimator, I'd like to simply use it (GridSearchCV.best_estimator_), with one change - now I'd like to give it all 24 processors for when I use it in fitting new data. Is there a way to change just this one parameter, or do I need to manually specify all the best parameters for the estimator in order to make this change? Not sure that actual code is helpful in this, but here's some sample code in case:

est = ensemble.RandomForestRegressor(...,
                                     n_jobs=4,
                                    )

gridsearch = GridSearchCV(estimator = est,
                          ...
                          n_jobs=6
                         )

gridsearch.fit(X,y)

best = gridsearch.best_estimator_

I'd like best to use all the processors, i.e. have n_jobs=24. But it has inherited n_jobs=4 from the original estimator. Any way to change just this parameter without re-specifying all the parameters?

This is my first question here, so please do let me know if there's anything obvious I've left out to make this a sensible question.


Solution

  • You can just use the set_params(**params) method.

    In your case...

    best = gridsearch.best_estimator_
    best.set_params(n_jobs=24)