From f676ecbf6a5fd0c9f6ec29f999892e3fa0578a52 Mon Sep 17 00:00:00 2001 From: Zach Carmichael Date: Tue, 8 May 2018 23:48:33 -0400 Subject: [PATCH 1/2] Added maximize parameter to CV init --- evolutionary_search/cv.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/evolutionary_search/cv.py b/evolutionary_search/cv.py index 74d6a9f..21f9d56 100644 --- a/evolutionary_search/cv.py +++ b/evolutionary_search/cv.py @@ -216,6 +216,10 @@ class EvolutionaryAlgorithmSearchCV(BaseSearchCV): FitFailedWarning is raised. This parameter does not affect the refit step, which will always raise the error. + maximize : boolean, default=True + If True, maximize the scoring function result. Otherwise, minimize + the scoring function result. + Examples -------- @@ -283,7 +287,7 @@ def __init__(self, estimator, params, scoring=None, cv=4, gene_mutation_prob=0.1, gene_crossover_prob=0.5, tournament_size=3, generations_number=10, gene_type=None, n_jobs=1, iid=True, error_score='raise', - fit_params={}): + fit_params={}, maximize=True): super(EvolutionaryAlgorithmSearchCV, self).__init__( estimator=estimator, scoring=scoring, fit_params=fit_params, iid=iid, refit=refit, cv=cv, verbose=verbose, @@ -302,7 +306,11 @@ def __init__(self, estimator, params, scoring=None, cv=4, self.best_params_ = None self.score_cache = {} self.n_jobs = n_jobs - creator.create("FitnessMax", base.Fitness, weights=(1.0,)) + if maximize: + weights = (1.0,) + else: + weights = (-1.0,) + creator.create("FitnessMax", base.Fitness, weights=weights) creator.create("Individual", list, est=clone(self.estimator), fitness=creator.FitnessMax) @property From cd5fcd44bac9370748105b6cdea6c178e3d0d4ac Mon Sep 17 00:00:00 2001 From: Zach Carmichael Date: Wed, 9 May 2018 22:46:35 -0400 Subject: [PATCH 2/2] Resolve issues with minimization functionality --- evolutionary_search/__init__.py | 1 + evolutionary_search/cv.py | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/evolutionary_search/__init__.py b/evolutionary_search/__init__.py index 60a2e6b..99e2652 100644 --- a/evolutionary_search/__init__.py +++ b/evolutionary_search/__init__.py @@ -1,2 +1,3 @@ from .cv import * from .optimize import maximize, compile + diff --git a/evolutionary_search/cv.py b/evolutionary_search/cv.py index 21f9d56..b6bc8f2 100644 --- a/evolutionary_search/cv.py +++ b/evolutionary_search/cv.py @@ -306,12 +306,16 @@ def __init__(self, estimator, params, scoring=None, cv=4, self.best_params_ = None self.score_cache = {} self.n_jobs = n_jobs + self.maximize = maximize if maximize: weights = (1.0,) + fitness_name = "FitnessMax" else: weights = (-1.0,) - creator.create("FitnessMax", base.Fitness, weights=weights) - creator.create("Individual", list, est=clone(self.estimator), fitness=creator.FitnessMax) + fitness_name = "FitnessMin" + creator.create(fitness_name, base.Fitness, weights=weights) + creator.create("Individual", list, est=clone(self.estimator), + fitness=getattr(creator, fitness_name)) @property def possible_params(self): @@ -357,7 +361,10 @@ def best_index_(self): def fit(self, X, y=None): self.best_estimator_ = None - self.best_mem_score_ = float("-inf") + if self.maximize: + self.best_mem_score_ = float("-inf") + else: + self.best_mem_score_ = float("inf") self.best_mem_params_ = None for possible_params in self.possible_params: _check_param_grid(possible_params) @@ -462,7 +469,8 @@ def _fit(self, X, y, parameter_dict): print("Best individual is: %s\nwith fitness: %s" % ( current_best_params_, current_best_score_)) - if current_best_score_ > self.best_mem_score_: + if ((self.maximize and (current_best_score_ > self.best_mem_score_)) or + (not self.maximize and (current_best_score_ < self.best_mem_score_))): self.best_mem_score_ = current_best_score_ self.best_mem_params_ = current_best_params_ @@ -476,3 +484,4 @@ def _fit(self, X, y, parameter_dict): self.best_score_ = current_best_score_ self.best_params_ = current_best_params_ +