Implementing Random Search
To implement Random Search (see Chapter 3) in Hyperopt, we can simply follow the steps explained in the previous section and pass the rand.suggest
object to the algo
parameter in the fmin()
function. Let’s learn how we can utilize the Hyperopt
package to perform Random Search. We will use the same data and sklearn
pipeline definition as in Chapter 7, Hyperparameter Tuning via Scikit, but with a slightly different definition of the hyperparameter space. Let’s follow the steps that were introduced in the previous section:
- Define the objective function to be minimized. Here, we are utilizing the defined pipeline,
pipe
, to calculate the 5-fold cross-validation score by utilizing thecross_val_score
function fromsklearn
. We will use the F1 score as the evaluation metric:import numpy as np from sklearn.base import clone from sklearn.model_selection import cross_val_score from hyperopt import STATUS_OK def objective(space): ...