fit xgboost model withg gridsearch over parameters in python

To fit an XGBoost model with grid search over parameters in Python, you can use the GridSearchCV class from the sklearn library. Here's an example:

main.py
from sklearn.model_selection import GridSearchCV
import xgboost as xgb

# Load the dataset and split it into training and testing
# X_train, y_train, X_test, y_test = ...

# Define the parameter grid for grid search
param_grid = {
    'max_depth': [3, 4, 5],
    'learning_rate': [0.1, 0.01, 0.001],
    'n_estimators': [100, 200, 300]
}

# Create an XGBoost classifier
xgb_model = xgb.XGBClassifier()

# Perform grid search
grid_search = GridSearchCV(xgb_model, param_grid, cv=3, scoring='accuracy')
grid_search.fit(X_train, y_train)

# Print the best hyperparameters
print("Best Hyperparameters: ", grid_search.best_params_)

# Fit the XGBoost model with the best hyperparameters
best_xgb_model = xgb.XGBClassifier(**grid_search.best_params_)
best_xgb_model.fit(X_train, y_train)

# Evaluate on test data
accuracy = best_xgb_model.score(X_test, y_test)
print("Accuracy: {:.2f}%".format(accuracy * 100))
905 chars
31 lines

In this example, we define the param_grid dictionary with the hyperparameters we want to tune. We then create an XGBoost classifier using xgb.XGBClassifier().

Next, we create a GridSearchCV object and pass it the XGBoost classifier, the parameter grid, the number of cross-validation folds (cv), and the scoring metric (scoring).

We then fit the grid search object to the training data using fit(X_train, y_train). After the grid search is complete, we output the best hyperparameters using grid_search.best_params_.

Finally, we create a new XGBoost classifier with the best hyperparameters and fit it to the training data. We evaluate the model's accuracy on the test data using score(X_test, y_test).

This way, we can find the best combination of hyperparameters for the XGBoost model using grid search in Python.

gistlibby LogSnag