Search code examples
gridsearchcv

Using GridSearchCV to find best hyper parameters


Hi I'm new to GridSearchCV and I'm trying to get the best parameters identified using the best_params_ attribute. It runs correctly, but it only returns a 'max_depth':3, when I'm expecting it to also return the best max_leaf_nodes and best min_samples_split as well. Please see my code below and let me know if I'm not doing or understanding something correctly. Thanks!

from sklearn.model_selection import GridSearchCV

param_grid = [
  {'max_depth': [1,2,3,4,5,8,16,32]},
  {'max_leaf_nodes': list(range(2, 20, 1))},
  {'min_samples_split': [2,3,4,5,8,12,16,20]},
 ]

# Call the fit() method to perform the grid search using 3-fold cross-validation.
grid_search_cv = GridSearchCV(DecisionTreeClassifier(random_state=42), param_grid, verbose=1, cv=3) 

# Fit the model to the training set 
grid_search_cv.fit(X_train, y_train)

print("The best parameters are: ", grid_search_cv.best_params_)

Output:  The best parameters are: {'max_depth': 3}


Solution

  • I figured it out. I shouldn't have a bracket on every line of my dictionary.