Skip to content

Instantly share code, notes, and snippets.

@p-m-m-c
Last active November 13, 2018 19:28
Show Gist options
  • Save p-m-m-c/7489e77436c72f0b873f1f1167a07412 to your computer and use it in GitHub Desktop.
Save p-m-m-c/7489e77436c72f0b873f1f1167a07412 to your computer and use it in GitHub Desktop.
Extracting scores from gridsearch object
def extract_results_GS(grid_scores):
"""
After applying gridsearch to a dataset, for all different combinations of parameters, an array of scores is
given in the grid_scores object of the estimator. The length of this array is equal to the number of fits of
the model. E.g. a model that is fit on three different train/test combinations, as happens in cross-validation. From that, the
mean validation score and standard deviation of the scores can be obtained. This function converts a
GridSearchCV.grid_scores_ object to a pandas DataFrame object. The frame is unsorted, because the best parameter
setting can be verified in the GridSearchCV.best_estimator_ and best_score_ properties.
"""
import numpy as np
import pandas as pd
scores = [item.mean_validation_score for item in grid_scores]
stddevs = [item.cv_validation_scores.std() for item in grid_scores]
parameter_sets = [item.parameters for item in grid_scores]
return pd.concat([pd.DataFrame(parameter_sets, index=np.arange(len(grid_scores))), pd.Series(scores, name='mean_val_score'),
pd.Series(stddevs, name='stddevs_val_score')], axis=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment