Hyperopt

from hyperopt import fmin, tpe, hp, STATUS_OK, STATUS_FAIL, Trials, space_eval

import numpy as np



def score(params):

model = XGBRegressor(**params)


model.fit(

X_train,

Y_train,

eval_set=[(X_train, Y_train), (X_valid, Y_valid)],

verbose=False,

early_stopping_rounds=10,

)

Y_pred = model.predict(X_valid).clip(0, 20)

score = sqrt(mean_squared_error(Y_valid, Y_pred))

print(score)

return {"loss": score, "status": STATUS_OK}



def optimize(trials):

space = {

"max_depth": hp.choice("max_depth", np.arange(10, 25, 1, dtype=int)),

"n_estimators": hp.choice(

"n_estimators", np.arange(1000, 10000, 10, dtype=int)

),

"colsample_bytree": hp.quniform("colsample_bytree", 0.5, 1.0, 0.1),

"min_child_weight": hp.choice(

"min_child_weight", np.arange(250, 350, 10, dtype=int)

),

"subsample": hp.quniform("subsample", 0.7, 0.9, 0.1),

"eta": hp.quniform("eta", 0.1, 0.3, 0.1),

"objective": "reg:squarederror",

"tree_method": "gpu_hist",

"eval_metric": "rmse",

}


best = fmin(score, space, algo=tpe.suggest, max_evals=10)

return best



trials = Trials()

best_params = optimize(trials)


space = {

"max_depth": hp.choice("max_depth", np.arange(10, 25, 1, dtype=int)),

"n_estimators": hp.choice("n_estimators", np.arange(1000, 10000, 10, dtype=int)),

"colsample_bytree": hp.quniform("colsample_bytree", 0.5, 1.0, 0.1),

"min_child_weight": hp.choice(

"min_child_weight", np.arange(250, 350, 10, dtype=int)

),

"subsample": hp.quniform("subsample", 0.7, 0.9, 0.1),

"eta": hp.quniform("eta", 0.1, 0.3, 0.1),

"objective": "reg:squarederror",

"tree_method": "gpu_hist",

"eval_metric": "rmse",

}

space_eval(space, best_params)