0

I am trying to run a RandomForest Classifier using Pipeline, GridSerach and CV

I am getting an error when I fit the data. I am not sure how to fix it. I found a similar Question with the solution https://stackoverflow.com/a/34890246/9592484 but didn't work for me

Will appreciate any help on this.

My code is:

 column_trans = make_column_transformer((OneHotEncoder(), ['CategoricalData']),
 remainder='passthrough')
 RF = RandomForestClassifier()
 pipe = make_pipeline(column_trans, RF)

 # Set grid search params
 grid_params = [{'randomforestclassifier_criterion': ['gini', 'entropy'],
 'randomforestclassifier_min_samples_leaf': [5,10,20,30,50,80,100],
 'randomforestclassifier_max_depth': [3,4,6,8,10],
 'randomforestclassifier_min_samples_split': [2,4,6,8,10]}]

 # Construct grid search
 gs = GridSearchCV(estimator = pipe,
 param_grid = grid_params,
 scoring='accuracy',
 cv=5)

 gs.fit(train_features, train_target)  ----This is where I get an error


ValueError: Invalid parameter randomforestclassifier_criterion for estimator Pipeline(steps=[('columntransformer',
             ColumnTransformer(remainder='passthrough',
                               transformers=[('onehotencoder',
                                              OneHotEncoder(),
                                              ['saleschanneltypeid'])])),
            ('randomforestclassifier', RandomForestClassifier())]). Check the list of available parameters with `estimator.get_params().keys()`.
TigSh
  • 615
  • 1
  • 6
  • 15

2 Answers2

1

The make_pipeline utility function derives step names from transformer/estimator class names. For example, the RandomForestClassifier is mapped to randomforestclassifier step.

Please adjust your grid search parameter prefixes acordingly (ie. from RF to randomforestclassifier). For example, RF__criterion should become randomforestclassifier__criterion.

user1808924
  • 4,563
  • 2
  • 17
  • 20
  • I am still getting the same error. I changed all RF in grid search to randomforestclassifier. – TigSh Jun 03 '20 at 17:24
  • What's the new error message? BTW, your original error message is incomplete, there could be some critical piece of information missing in your question/code example. – user1808924 Jun 03 '20 at 19:06
  • Also, I didn't know you can access the parameters of steps of a pipeline only by using '__’ – TigSh Jun 03 '20 at 23:11
1

You are not correctly prepending the keyword. You have to use 2 underscore ( __ ) before each and every parameter. You are using only 1 underscore ( _ ) which wont work.

spectre
  • 717
  • 7
  • 21