-
Notifications
You must be signed in to change notification settings - Fork 1
/
estimator_select.py
33 lines (29 loc) · 1.32 KB
/
estimator_select.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import pandas as pd
from sklearn.model_selection import GridSearchCV
import numpy as np
class EstimatorSelectionHelper:
def __init__(self, models, params):
if not set(models.keys()).issubset(set(params.keys())):
missing_params = list(set(models.keys()) - set(params.keys()))
raise ValueError("Some estimators are missing parameters: %s" % missing_params)
self.models = models
self.params = params
self.keys = models.keys()
self.grid_searches = {}
def fit(self, X, y, cv=3, n_jobs=1, verbose=1, scoring=None, refit=False):
for key in self.keys:
print("Running GridSearchCV for %s." % key)
model = self.models[key]
params = self.params[key]
gs = GridSearchCV(model, params, cv=cv, n_jobs=n_jobs, verbose=verbose, scoring=scoring, refit=refit, return_train_score=False)
gs.fit(X, y)
self.grid_searches[key] = gs
def score_summary(self, sort_by='mean_score'):
df = None
for k in self.keys:
self.grid_searches[k].cv_results_['estimator'] = k
if df is not None :
df = pd.concat([df, pd.DataFrame(self.grid_searches[k].cv_results_)])
else:
df = pd.DataFrame(self.grid_searches[k].cv_results_)
return df