Search code examples
pythonscikit-learnrandom-forestgrid-search

Attribute Error: 'RandomForestRegressor' object has no attribute 'best_params_'


I am getting this Error while doing Grid-Search for my Classification with Random Forest.

from sklearn.ensemble import RandomForestRegressor
rf2 = RandomForestRegressor(random_state = 50)
rf2.fit(X_train1, y_train1)

### Grid Search ###
num_leafs = [1, 5, 10, 20, 50, 100]

parameters3 = [{'n_estimators' : range(100,800,20),
             'max_depth': range(1,20,2),
             'min_samples_leaf':num_leafs
             }]


gs3 = GridSearchCV(estimator=rf2,
                  param_grid=parameters3,
                  cv = 10,
                  n_jobs = -1)

gs3 = rf2.fit(X_train1, y_train1)

gs3.best_params_ # <- thats where I get the Error

I don't know the Problem because it worked the same way (different parameters of course) with a SVM and a Decision Tree.

Thanks in advance


Solution

  • Replace this: gs3 = rf2.fit(X_train1, y_train1)

    by this: gs3.fit(X_train1, y_train1)

    Then you'll be able to use: gs3.best_params_

    You error was caused by the fact that you reassigned gs3 to the RandomForest() call, so gs3 wasn't a GridSearchCV object anymore.