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 74d6a9f..b6bc8f2 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,8 +306,16 @@ 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,)) - creator.create("Individual", list, est=clone(self.estimator), fitness=creator.FitnessMax) + self.maximize = maximize + if maximize: + weights = (1.0,) + fitness_name = "FitnessMax" + else: + weights = (-1.0,) + 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): @@ -349,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) @@ -454,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_ @@ -468,3 +484,4 @@ def _fit(self, X, y, parameter_dict): self.best_score_ = current_best_score_ self.best_params_ = current_best_params_ +