XGBoosting Home | About | Contact | Examples

Save XGBoost Model Using skops Library

The skops library simplifies saving and loading XGBoost models.

This means you can train your model in Python and potentially deploy it in another language like Java or C++.

First, we must install the skops library using our preferred package manager, such as pip:

pip install skops

Next, we can save our XGBoost model using skops.

from sklearn.datasets import make_classification
from xgboost import XGBClassifier
from sklearn.metrics import accuracy_score
import skops.io as sio

# 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 skops format
sio.dump(model, 'xgb_model.skops')

# Load model from file
loaded_model = sio.load('xgb_model.skops', trusted=True)

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

# Verify the loaded model performs the same as the original
print(f"Accuracy: {accuracy_score(y, predictions):.4f}")

Here’s a step-by-step breakdown:

  1. We train an XGBClassifier on a randomly generated binary classification dataset.
  2. We save the trained model in skops format using skops’ dump() function.
  3. We load the saved model using skops’ load() function.
  4. We use the loaded model to make predictions on the original dataset.
  5. We print the accuracy score to verify that the loaded model performs identically to the original model.


See Also