Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added maximize parameter to CV init #51

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions evolutionary_search/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from .cv import *
from .optimize import maximize, compile

27 changes: 22 additions & 5 deletions evolutionary_search/cv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------
Expand Down Expand Up @@ -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,
Expand All @@ -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):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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_

Expand All @@ -468,3 +484,4 @@ def _fit(self, X, y, parameter_dict):

self.best_score_ = current_best_score_
self.best_params_ = current_best_params_