XGBoosting Home | About | Contact | Examples

Configure XGBoost "xgb_model" Parameter

The xgb_model parameter in XGBoost’s native API allows you to continue training from a previously saved XGBoost model.

This is particularly useful when you want to perform iterative training or fine-tune an existing model on new data without starting from scratch.

In this example, we’ll demonstrate how to use the xgb_model parameter with xgboost.train() to load a saved model and continue training it on the same dataset.

import numpy as np
import xgboost as xgb

# Generate a synthetic dataset for binary classification
num_samples = 1000
num_features = 10
X = np.random.rand(num_samples, num_features)
y = np.random.randint(2, size=num_samples)

# Convert data to DMatrix
dtrain = xgb.DMatrix(X, label=y)

# Set training parameters
params = {
    'objective': 'binary:logistic',
    'eval_metric': 'error',
    'eta': 0.1,
    'max_depth': 3,
    'num_parallel_tree': 1,
    'subsample': 0.8,
    'colsample_bytree': 0.8,
    'seed': 42

# Train an initial model for 10 rounds and save it
initial_rounds = 10
initial_model = xgb.train(params, dtrain, num_boost_round=initial_rounds)

# Load the saved model using xgb_model and continue training for 10 more rounds
additional_rounds = 10
final_model = xgb.train(params, dtrain, num_boost_round=additional_rounds,

# Make predictions and evaluate the final model
predictions = final_model.predict(dtrain)
predictions = np.round(predictions).astype(int)
accuracy = np.sum(predictions == y) / num_samples
print(f"Final model accuracy: {accuracy:.2f}")

In this example, we first generate a synthetic binary classification dataset using NumPy. We then convert the data to an xgb.DMatrix and set the training parameters.

We train an initial XGBoost model for 10 rounds using xgb.train() and save it to a file named initial_model.json using the save_model() method.

To continue training from the saved model, we call xgb.train() again, but this time we pass the path to the saved model using the xgb_model parameter. We train for an additional 10 rounds, effectively fine-tuning the initial model.

Finally, we make predictions using the final model and calculate the accuracy to evaluate its performance.

By utilizing the xgb_model parameter, you can easily load a previously saved XGBoost model and continue training it, allowing for iterative model improvement and fine-tuning without the need to start training from zero each time.

See Also