Search code examples
pythonpython-3.xscikit-learnrandom-forestgrid-search

GridSearchCV Error on param_grid With RandomForestRegressor


ValueError: Invalid parameter estimator for estimator RandomForestRegressor().
Check the list of available parameters with `estimator.get_params().keys()`.

This is the error i get while using GridSearchCV on a RandomForestRegressor model. Here's the code

%%time 
from sklearn.model_selection import RandomizedSearchCV

rf_grid= {"estimator__n_estimators ": np.arange(10,100,10),
      "estimator__max_depth ": [None,3,5,10],
      "estimator__min_sample_split": np.arange(2,20,2),
      "estimator__min_sample_leaf" : np.arange(1,20,2),
      "estimator__max_features ": [0.5,1,'sqrt','auto'],
      "estimator__max_samples" : [10000]
}
rfr_2=RandomForestRegressor()

rs_model= RandomizedSearchCV(estimator=rfr_2,
                        param_distributions=rf_grid,
                        n_iter=100,
                        cv=5,
                        verbose= True)

rs_model.fit(X_train,Y_train)

Solution

  • So Turns out I'm supposed to use single quotes ' ' instead of double " " .

     %%time 
     from sklearn.model_selection import RandomizedSearchCV
    
     rf_grid= {'n_estimators': np.arange(10,100,10),
          'max_depth': [None,3,5,10],
          'min_samples_split': np.arange(2,20,2),
          'min_samples_leaf' : np.arange(1,20,2),
          'max_features': [0.5,1,'sqrt','auto'],
          'max_samples' : [100]
      }
    
      rs_model= RandomizedSearchCV(rfr,
                            param_distributions=rf_grid,
                            n_iter=100,
                            cv=5,
                            verbose= True)
      rs_model.get_params()
      }