rsteca / sklearn-deap

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

Error using evolutionary search for MLPRegressor Parameter optimization #32

Closed astha234 closed 6 years ago

astha234 commented 6 years ago

Following is my code: from evolutionary_search import EvolutionaryAlgorithmSearchCV from sklearn.model_selection import StratifiedKFold

paramgrid = {"activation": ["identity", "logistic", "tanh", "relu"], "solver" : ["lbfgs", "sgd", "adam"], "hidden_layer_sizes" : [1,2,3,4], "max_iter" : [100,150]}

cv = EvolutionaryAlgorithmSearchCV(estimator=MLPRegressor(), params=paramgrid, scoring="accuracy", cv=StratifiedKFold(n_splits=2), verbose=1, population_size=50, gene_mutation_prob=0.10, gene_crossover_prob=0.5, tournament_size=3, generations_number=5, n_jobs=4) cv.fit(x,y)

The Error I get:

ValueErrorTraceback (most recent call last)

in () 20 generations_number=5, 21 n_jobs=4) ---> 22 cv.fit(x,y) /usr/share/anaconda2/lib/python2.7/site-packages/evolutionary_search/cv.pyc in fit(self, X, y) 350 for possible_params in self.possible_params: 351 _check_param_grid(possible_params) --> 352 self._fit(X, y, possible_params) 353 if self.refit: 354 self.best_estimator_ = clone(self.estimator) /usr/share/anaconda2/lib/python2.7/site-packages/evolutionary_search/cv.pyc in _fit(self, X, y, parameter_dict) 419 pop, logbook = algorithms.eaSimple(pop, toolbox, cxpb=0.5, mutpb=0.2, 420 ngen=self.generations_number, stats=stats, --> 421 halloffame=hof, verbose=self.verbose) 422 423 # Save History /usr/share/anaconda2/lib/python2.7/site-packages/deap/algorithms.pyc in eaSimple(population, toolbox, cxpb, mutpb, ngen, stats, halloffame, verbose) 145 # Evaluate the individuals with an invalid fitness 146 invalid_ind = [ind for ind in population if not ind.fitness.valid] --> 147 fitnesses = toolbox.map(toolbox.evaluate, invalid_ind) 148 for ind, fit in zip(invalid_ind, fitnesses): 149 ind.fitness.values = fit /usr/share/anaconda2/lib/python2.7/multiprocessing/pool.pyc in map(self, func, iterable, chunksize) 249 ''' 250 assert self._state == RUN --> 251 return self.map_async(func, iterable, chunksize).get() 252 253 def imap(self, func, iterable, chunksize=1): /usr/share/anaconda2/lib/python2.7/multiprocessing/pool.pyc in get(self, timeout) 565 return self._value 566 else: --> 567 raise self._value 568 569 def _set(self, i, obj): ValueError: continuous is not supported
rsteca commented 6 years ago

I guess it's because MLPRegressor is for doing regression and not classification. Does it work if you try to use GridSearchCV instead of EvolutionaryAlgorithmSearchCV?

astha234 commented 6 years ago

yes. I got GridSearchCV to work: image

rsteca commented 6 years ago

You can't use the "scoring" parameter with 'accuracy' for MLPRegressor, you should leave it as None in the same way you did with GridSearchCV