Skip to content

Instantly share code, notes, and snippets.

@willwest
Last active January 27, 2023 11:32
Show Gist options
  • Save willwest/fcb61b110b9f7f59db40 to your computer and use it in GitHub Desktop.
Save willwest/fcb61b110b9f7f59db40 to your computer and use it in GitHub Desktop.
Export a CSV file of GridSearchCV scores.
import csv
import numpy as np
from sklearn.grid_search import GridSearchCV
def print_GridCV_scores(gs_clf, export_file):
'''Exports a CSV of the GridCV scores.
gs_clf: A GridSearchCV object which has been fitted
export_file: A file path
Example output (file content):
mean,std,feat__words__ngram_range,feat__words__stop_words,clf__alpha
0.56805074971164937,0.0082019735998974941,"(1, 2)",english,0.0001
0.57189542483660127,0.0066384723824170488,"(1, 2)",None,0.0001
0.56839677047289505,0.0082404203511470264,"(1, 3)",english,0.0001
0.57306164295783668,0.0095988722286300399,"(1, 3)",None,0.0001
0.53524285531205951,0.0026015635012174854,"(1, 2)",english,0.001
0.53742150454953219,0.0031141868512110649,"(1, 2)",None,0.001
0.53510829168268614,0.0032487504805843725,"(1, 3)",english,0.001
0.53717160066641034,0.0034025374855825019,"(1, 3)",None,0.001
'''
with open(export_file, 'w') as outfile:
csvwriter = csv.writer(outfile, delimiter=',')
# Create the header using the parameter names
header = ["mean","std"]
param_names = [param for param in gs_clf.param_grid]
header.extend(param_names)
csvwriter.writerow(header)
for config in gs_clf.grid_scores_:
# Get mean and standard deviation
mean = config[1]
std = np.std(config[2])
row = [mean,std]
# Get the list of parameter settings and add to row
params = [str(p) for p in config[0].values()]
row.extend(params)
csvwriter.writerow(row)
@egemenertugrul
Copy link

grid_scores_ is deprecated, it is cv_results_ now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment