rsteca / sklearn-deap

Use evolutionary algorithms instead of gridsearch in scikit-learn
MIT License
767 stars 132 forks source link

Minimizing a loss function #49

Open jCrompton opened 6 years ago

jCrompton commented 6 years ago

Line 305 in cv.py, is there anyway we can change the weights tuple to minimize a loss function rather than always maximize ?

jCrompton commented 6 years ago

basically I want to change creator.create("FitnessMax", base.Fitness, weights=(1.0,)) to creator.create("FitnessMax", base.Fitness, weights=(-1.0,))

craymichael commented 6 years ago

When creating the cv, you can wrap the scoring function parameter. For example:

from functools import wraps
from sklearn import accuracy_score
from evolutionary_search import EvolutionaryAlgorithmSearchCV
...
def wrap_score_func(score_func):
    @wraps(score_func)
    def wrapped(*args, **kwargs):
        return -score_func(*args, **kwargs)
    return wrapped
...
cv = EvolutionaryAlgorithmSearchCV(estimator=SVC(),
                                   params=paramgrid,
                                   scoring=wrap_score_func(accuracy_score),
                                   ...)

Ideally, this should just be a parameter during cv initialization.

craymichael commented 6 years ago

Created PR #51