scikit-learn超参数调优 (自动寻找模型最佳参数) 方法
from deap import base, creator, tools, algorithms
from sklearn.svm import SVC
from sklearn.model_selection import cross_val_score
def eval_params(params):
model = SVC(**params)
score = cross_val_score(model, X_train, y_train, cv=5).mean()
return score,
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", list, fitness=creator.FitnessMax)
toolbox = base.Toolbox()
toolbox.register("attr_C", random.uniform, 0.1, 10)
toolbox.register("attr_kernel", random.choice, ['linear', 'rbf'])
toolbox.register("individual", tools.initCycle, creator.Individual,
(toolbox.attr_C, toolbox.attr_kernel), n=1)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("evaluate", eval_params)
toolbox.register("mate", tools.cxTwoPoint)
toolbox.register("mutate", tools.mutGaussian, mu=0, sigma=1, indpb=0.1)
toolbox.register("select", tools.selTournament, tournsize=3)
population = toolbox.population(n=10)
algorithms.eaSimple(population, toolbox, cxpb=0.5, mutpb=0.2, ngen=10)
best_individual = tools.selBest(population, 1)[0]
print(best_individual)