gridsearch xgboost regressor sklearn in python

You can use GridSearchCV class from sklearn to perform a grid search for hyperparameter tuning of XGBoost regressor. Here's an example code:

main.py
from sklearn.model_selection import GridSearchCV
from xgboost import XGBRegressor

# create XGBoost regressor object
xgb = XGBRegressor()

# define hyperparameter grid
param_grid = {
    'n_estimators': [100, 200, 300],
    'max_depth': [3, 4, 5],
    'learning_rate': [0.1, 0.01, 0.001],
    'gamma': [0, 0.1, 0.01],
    'colsample_bytree': [0.5, 0.7, 0.9, 1],
    'subsample': [0.5, 0.7, 0.9, 1]
}

# perform grid search
grid_search = GridSearchCV(estimator=xgb, param_grid=param_grid, cv=5,
                          n_jobs=-1, verbose=2, scoring='neg_mean_squared_error')
grid_search.fit(X_train, y_train)

# print best hyperparameters and score
print("Best Hyperparameters: ", grid_search.best_params_)
print("Best Score: ", (-1*grid_search.best_score_))
760 chars
25 lines

Here, we defined XGBRegressor() object and then defined a hyperparameter search space for n_estimators, max_depth, learning_rate, gamma, colsample_bytree, and subsample parameters. Then GridSearchCV is used to perform a 5-fold cross-validation grid search for the best hyperparameters. Finally, we printed the best hyperparameters and their corresponding best score.

gistlibby LogSnag