XGBoosting Home | About | Contact | Examples

Configure XGBoost "num_boost_round" Parameter

The num_boost_round parameter in XGBoost controls the number of boosting iterations the model performs during training.

Setting this parameter appropriately is crucial for achieving optimal model performance and avoiding overfitting or underfitting.

When using the XGBoost native API with xgboost.train(), you can specify the num_boost_round parameter to determine the number of boosting rounds the model will execute. Here’s an example of how to configure this parameter:

from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, roc_auc_score
import xgboost as xgb

# Generate a synthetic binary classification dataset
X, y = make_classification(n_samples=1000, n_features=10, n_informative=5, n_redundant=2, random_state=42)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Convert data to DMatrix format
dtrain = xgb.DMatrix(X_train, label=y_train)

# Define XGBoost parameters
params = {
    'objective': 'binary:logistic',
    'learning_rate': 0.1,
    'max_depth': 3,
    'subsample': 0.8,
    'colsample_bytree': 0.8,

# Train the model with num_boost_round set to 100
num_boost_round = 100
model = xgb.train(params, dtrain, num_boost_round=num_boost_round)

# Make predictions on test data
dtest = xgb.DMatrix(X_test)
predictions = model.predict(dtest)

# Print model performance metrics
print(f"Accuracy: {accuracy_score(y_test, predictions > 0.5):.3f}")
print(f"AUC: {roc_auc_score(y_test, predictions):.3f}")

In this example, we set num_boost_round to 100, which means the model will perform 100 boosting iterations. The optimal value for this parameter depends on the complexity of your dataset and the other hyperparameters you’ve chosen. Increasing num_boost_round generally improves performance up to a certain point, after which the model may start to overfit.

To find the best num_boost_round value, you can start with a relatively small number and gradually increase it while monitoring the model’s performance on a validation set. Look for the point at which the validation metrics stop improving or start to degrade, as this indicates overfitting.

Keep in mind that higher num_boost_round values will also increase training time. If computational resources or time constraints are a concern, you may need to balance model performance with training efficiency.

Another approach is to use early stopping, which automatically determines the optimal num_boost_round value based on a specified validation metric. Early stopping halts training when the validation metric stops improving for a given number of rounds, helping to prevent overfitting and saving computational resources.

By carefully tuning the num_boost_round parameter in XGBoost, you can find the sweet spot between model performance and training efficiency, ultimately leading to better results on your machine learning tasks.

See Also