Created
February 11, 2020 22:40
-
-
Save kbfreder/1158fc1cf087c01b13e13f7178914140 to your computer and use it in GitHub Desktop.
Using sklearn's GridSearchCV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.model_selection import GridSearchCV | |
from sklearn.metrics import make_scorer | |
# import Random Forest | |
param_grid = {'n_estimators': [50, 100, 200], | |
'max_depth':[None, 10, 15, 20], | |
'criterion': ['gini', 'entropy'], | |
'min_impurity_decrease': [0, 1e7, 1e5]} | |
scorer = make_scorer(accuracy_score) | |
clf_grid = GridSearchCV(RandomForestClassifier(), param_grid, scoring=scorer, n_jobs=-1, | |
return_train_score=True, cv=3) | |
clf_grid.fit(train_X, train_y) | |
# to retrieve the best estimator: | |
clf_best = clf_grid.best_estimator_ | |
# if you want to examine the results: | |
grid_res_df = pd.DataFrame.from_dict(clf_grid.cv_results_) | |
grid_res_df.sort_values(by='mean_test_score', ascending=False, inplace=True) | |
# then for example: | |
grid_res_df[['mean_test_score', 'mean_train_score']].head() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment