XGBoosting Home | About | Contact | Examples

Deploy XGBoost Model As Service with Flask

Deploying your trained XGBoost model as a web service allows you to make predictions on new data through a simple API, making it easier to integrate your model into applications.

This example demonstrates how to save a trained XGBoost model and create a Flask web application that loads the model and serves predictions via a REST API.

First, let’s train an XGBoost model on a synthetic dataset and save it to a file:

import joblib
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from xgboost import XGBClassifier

# Generate a synthetic dataset
X, y = make_classification(n_samples=1000, n_classes=2, n_features=10, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train an XGBClassifier
model = XGBClassifier(n_estimators=100, learning_rate=0.1, max_depth=3, random_state=42)
model.fit(X_train, y_train)

# Save the trained model to a file
joblib.dump(model, 'xgb_model.pkl')

Now that we have a trained model saved to a file, let’s create a Flask application that loads the model and serves predictions.

First, you may need to install flask using your preferred Python package manager, such as pip:

pip install flask

We can then develop the Flask program that provides a web service for making predictions via HTTP requests.

import joblib
from flask import Flask, request, jsonify
import numpy as np

app = Flask(__name__)

# Load the trained model
model = joblib.load('xgb_model.pkl')

@app.route('/predict', methods=['POST'])
def predict():
    # Get the input data from the request
    data = request.get_json(force=True)
    X = np.array(data['features'])

    # Make predictions using the loaded model
    y_pred_proba = model.predict_proba(X)
    y_pred = model.predict(X)

    # Return the predictions as a JSON response
    response = {
        'probabilities': y_pred_proba.tolist(),
        'predictions': y_pred.tolist()
    return jsonify(response)

if __name__ == '__main__':
    app.run(port=5000, debug=True)

In this Flask application, we load the saved XGBoost model using joblib.load().

We then define a /predict endpoint that accepts POST requests with JSON data containing the input features. The predict() function extracts the feature data from the JSON payload, uses the loaded model to make predictions, and returns the predicted class probabilities and labels as a JSON response.

To make predictions, you can send a POST request to the /predict endpoint with the input features as a JSON payload. For example, using curl:

curl -X POST -H "Content-Type: application/json" -d '{"features": [[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]]}' http://localhost:5000/predict

This request sends a single data point with 10 features to the /predict endpoint.

The server will respond with a JSON object containing the predicted class probabilities and the predicted class label and class probabilities:

  "predictions": [
  "probabilities": [

By following this example, you can easily deploy your trained XGBoost model as a web service using Flask, allowing you to make predictions on new data through a simple API.

This approach can be extended to handle more complex input data and can be integrated with other tools and frameworks to build end-to-end machine learning applications.

See Also