XGBoosting Home | About | Contact | Examples

How to Use XGBoost EvaluationMonitor Callback

The EvaluationMonitor callback in XGBoost provides a convenient way to monitor the performance of your model during training.

By using this callback, you can track evaluation metrics on both training and validation sets.

In this example, we’ll demonstrate how to use the EvaluationMonitor callback to monitor the training progress.

from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from sklearn.metrics import root_mean_squared_error
import xgboost as xgb

# Prepare dataset
X, y = make_regression(n_samples=1000, n_features=10, noise=0.1, random_state=42)

# Split dataset into train and validation sets
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)

# Define model parameters
params = {
    "objective": "reg:squarederror",
    "learning_rate": 0.1,
    "max_depth": 3,
    "subsample": 0.8,
    "colsample_bytree": 0.8,
}

# Prepare data
dtrain = xgb.DMatrix(X_train, label=y_train)
dval = xgb.DMatrix(X_val, label=y_val)

# Define evaluation sets
eval_sets = [(dtrain, "train"), (dval, "validation")]

# Create EvaluationMonitor callback
eval_monitor = xgb.callback.EvaluationMonitor()

# Train the model with the callback
model = xgb.train(
    params,
    dtrain,
    num_boost_round=1000,
    evals=eval_sets,
    callbacks=[eval_monitor],
)

# Make predictions on the validation set
y_pred = model.predict(dval)

# Calculate RMSE
rmse = root_mean_squared_error(y_val, y_pred)
print(f"Validation RMSE: {rmse:.4f}")

In this example, we first prepare a sample dataset using make_regression from scikit-learn and split it into training and validation sets. We then define the XGBoost model parameters and create DMatrix objects for the training and validation data.

Next, we define the evaluation sets as a list of tuples, where each tuple contains a DMatrix object and a string identifier (e.g., “train” or “validation”).

We create an instance of the EvaluationMonitor callback, using the default parameter values that will report the metric performance on each evaluation dataset each algorithm iteration.

Finally, we train the XGBoost model using xgb.train(), passing the model parameters, training data, number of boosting rounds, evaluation sets, and the EvaluationMonitor callback. During training, the callback will print the evaluation metrics for each round.

After training, we make predictions on the validation set and calculate the RMSE to assess the model’s performance.

By using the EvaluationMonitor callback, you can easily monitor the performance of your XGBoost model during training, ultimately helping you build better-performing models.



See Also