Klausos Klausos
Klausos Klausos

Reputation: 16060

AttributeError: 'GridSearchCV' object has no attribute 'best_estimator_'

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?

Upvotes: 4

Views: 11838

Answers (1)

Sean Easter
Sean Easter

Reputation: 879

It's not clear, from the code excerpt, how you set refit. Per the docs, best_estimator_ is only available when this is True. If False, you should still be able to find the best-performing parameters from grid_scores_, then use them with set_params().

Upvotes: 4

Related Questions