You can create your own GridSearch using ParameterGrid
.
For example:
from sklearn.model_selection import ParameterGrid
param_grid = {'a': [1, 2], 'b': [True, False]}
param_candidates = ParameterGrid(param_grid)
print(f'{len(param_candidates)} candidates')
results = []
for i, params in enumerate(param_candidates):
model = estimator.set_params(**params)
model.fit(X_train, y_train)
score = model.score(X_val, y_val)
results.append([params, score])
print(f'{i+1}/{len(param_candidates)}: ', params, score)
print(max(results, key=lambda x: x[1]))
To increase performance I would suggest parallelizing the loop:
from joblib import Parallel, delayed
param_grid = {'a': [1, 2], 'b': [True, False]}
param_candidates = ParameterGrid(param_grid)
print(f'{len(param_candidates)} candidates')
def fit_model(params):
model = estimator.set_params(**params)
model.fit(X_train, y_train)
score = model.score(X_val, y_val)
return [params, score]
results = Parallel(n_jobs=-1)(delayed(fit_model)(params) for params in param_candidates)
print(max(results, key=lambda x: x[1]))