Skip to content

Instantly share code, notes, and snippets.

@joshreini1
Created August 30, 2022 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshreini1/7436d9741233ad880db76c4359101a77 to your computer and use it in GitHub Desktop.
Save joshreini1/7436d9741233ad880db76c4359101a77 to your computer and use it in GitHub Desktop.
Tune hyperparameters for airbnb
#define the objective function and the hyperparameters we want to tune
def objective(space):
model=xgb.XGBRegressor(
n_estimators =space['n_estimators'],
max_depth = int(space['max_depth']),
gamma = space['gamma'],
reg_alpha = int(space['reg_alpha']),
min_child_weight=int(space['min_child_weight'])
)
evaluation = [( X_train_post, y_train), ( X_test_post, y_test)]
model.fit(X_train_post, y_train,
eval_set=evaluation, eval_metric="mae",
early_stopping_rounds=10,verbose=False)
pred = model.predict(X_test_post)
rmse= math.sqrt(mean_squared_error(y_test, pred))
print ("SCORE:", rmse)
return {'loss':rmse, 'status': STATUS_OK, 'model': model}
trials = Trials()
best_hyperparams = fmin(fn = objective,
space = space,
algo = tpe.suggest,
max_evals = 100,
trials = trials)
print("The best hyperparameters are : ","\n")
print(best_hyperparams)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment