Search code examples
pythonmachine-learningscikit-learnhyperparameters

Define log uniform learning_rate parameter in RandomizedSearchCV


I'm doing hyperparameter tuning and I'm using scikit-optimize for Bayesian optimization and RandomizedSearchCV for a randomized search.

In sci-kit optimize I can define the learning_rate easily it like this:

space= [Real(10**-5, 10**0, "log-uniform", name='learning_rate'),
        Integer(1, 20, name='max_depth'),
        ...
       ]

How would I do it with RandomizedSearchCV (sklearn) so that the same "list" of values is used for the optimization?

params_randomSearch = {
              "learning_rate" : TODO,
              "min_samples_leaf": np.arange(1,30,1),
              ..
              }

Solution

  • As per the documentation in RandomizedSearchCV():

    param_distributions : dict

    Dictionary with parameters names (string) as keys and distributions or 
    lists of parameters to try. Distributions must
    provide a rvs method for sampling (such as those from
    scipy.stats.distributions). If a list is given, it is sampled
    uniformly.
    

    Now, Real and Integer in scikit-optimize already implement the rvs() method, so you can use them directly. Just assign them in the dictionary to be used in RandomizedSearchCV.

    params_randomSearch = {
          "learning_rate" : Real(10**-5, 10**0, "log-uniform", name='learning_rate'),
          "min_samples_leaf": np.arange(1,30,1),
                  ..
                  }