How to use scikit-learn optimize in a class (especially the use_named_args decorator)?

The problem of self not being defined is unrelated to scikit.learn. You cannot use self to define a decorator, because it is only defined inside the method you are decorating. But even if you sidestep this issue (e.g. by providing param_space as a global variable) I expect the next problem will be that self will be passed to the use_named_args decorator, but it expects only arguments to be optimized.

The most obvious solution would be to not use the decorator on the fitness method but to define a decorated function that calls the fitness method, inside the find_best_model method, like this:

   def find_best_model(self):
         @use_named_args(dimensions=self.param_space)
         def fitness_wrapper(*args, **kwargs):
             return self.fitness(*args, **kwargs)
         search_result = gp.minimize(func=fitness_wrapper, dimensions=self.param_space,acq_func='EI',n_calls=10)
         return search_result

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top