Skip to content

Instantly share code, notes, and snippets.

@absent1706
Created April 7, 2019 08:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save absent1706/8aef7068422f702c37bc87749e1afc13 to your computer and use it in GitHub Desktop.
Save absent1706/8aef7068422f702c37bc87749e1afc13 to your computer and use it in GitHub Desktop.
datascience-visualize-grid-cv
def visualize_grid_cv_params(grid_cv):
df = pd.DataFrame(grid_cv.cv_results_['params'])
df['score'] = grid_cv.cv_results_['mean_test_score']
fig, axes = plt.subplots(1, len(grid_cv.param_grid), sharey=True, figsize=(15,4))
i = 0
for param in grid_cv.param_grid:
data = df.groupby(param).mean()['score'].to_dict()
param_values = list(data.keys())
score_values = list(data.values())
ax = axes[i]
ax.plot(param_values, score_values, marker='o')
ax.set_xlabel(param)
ax.set_ylabel('Score')
ax.grid(True)
i = i+1
# Example:
import pandas as pd
import sklearn
from sklearn import model_selection, ensemble
%pylab inline
param_grid = {
'n_estimators' : [5,10,15,50,100],
'max_features' : [5, 10, 20, 40, 50, X.shape[1]],
'max_depth' : [2,5,10,20,100],
}
random_forest_estimator = ensemble.RandomForestClassifier()
grid_cv = model_selection.GridSearchCV(random_forest_estimator, param_grid, scoring = 'accuracy', cv=3, return_train_score=True, verbose=2)
grid_cv.fit(X, Y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment