I am using grid search to tune parameters of my models (Random Forest, Linear Regression, etc.). So I save gs objects in grid_searches:
gs = GridSearchCV(model, params, cv=cv, n_jobs=n_jobs,
verbose=verbose, scoring="mean_squared_error", refit=refit)
gs.fit(trainX,trainy)
grid_searches[key] = gs
Then I want to access the best estimator for each model in order to make predictions:
def predict(testX, testy, grid_searches):
keys = models.keys()
for k in keys:
print("Predicting with %s." % k)
yhat = grid_searches[k].best_estimator_.predict(testX)
The error is the following:
AttributeError: 'GridSearchCV' object has no attribute 'best_estimator_'
So how should I make predictions using best models found by Grid Search?
grid_searches[k].predict(testX). After you callgs.fit(...),gsshould have the optimized parameters (based on the search space). What happens if you try justgrid_searches[k].predict(...)? Also, what happens if you add a line undergs.fit(...)withprint gs.best_estimator_?