4

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?

1
  • You should just be able to just use grid_searches[k].predict(testX). After you call gs.fit(...), gs should have the optimized parameters (based on the search space). What happens if you try just grid_searches[k].predict(...)? Also, what happens if you add a line under gs.fit(...) with print gs.best_estimator_? Commented Dec 9, 2015 at 21:50

1 Answer 1

4

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().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.