Created
August 30, 2022 19:50
-
-
Save joshreini1/7436d9741233ad880db76c4359101a77 to your computer and use it in GitHub Desktop.
Tune hyperparameters for airbnb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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