XGBoosting Home | About | Contact | Examples

Save XGBoost Model to UBJ Format in scikit-learn

UBJ (Universal Binary JSON) is a binary format for storing data that’s more compact and faster to parse than JSON.

Saving your XGBoost models in UBJ format can improve performance when loading and using the models.

The xgboost library provides built-in support for saving and loading models in UBJ format.

from sklearn.datasets import make_classification
from xgboost import XGBClassifier

# Generate a random classification dataset
X, y = make_classification(n_samples=1000, n_classes=2, random_state=42)

# Train an XGBoost model
model = XGBClassifier(n_estimators=100, learning_rate=0.1, random_state=42)
model.fit(X, y)

# save the model in ubj format

# Load model from file
loaded_model = XGBClassifier()

# Use the loaded model to make predictions
predictions = loaded_model.predict(X)

Here’s what we’re doing:

  1. We fit the model on the dataset.
  2. We save the fit model in UBJ format using save_model() to the file xgb_model.ubj.
  3. We create a new XGBClassifier model and populate it with the fit model details from xgb_model.ubj using load_model().
  4. We use the loaded model to make a prediction

See Also