Convert a pipeline with a XGBoost model

sklearn-onnx only converts scikit-learn models into ONNX but many libraries implement scikit-learn API so that their models can be included in a scikit-learn pipeline. This example considers a pipeline including a XGBoost model. sklearn-onnx can convert the whole pipeline as long as it knows the converter associated to a XGBClassifier. Let’s see how to do it.

Train a XGBoost classifier

import os
import numpy
import matplotlib.pyplot as plt
import onnx
from import GetPydotGraph, GetOpNodeProducer
import onnxruntime as rt
import sklearn
from sklearn.datasets import load_iris
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
import xgboost
from xgboost import XGBClassifier
import skl2onnx
from skl2onnx.common.data_types import FloatTensorType
from skl2onnx import convert_sklearn, update_registered_converter
from skl2onnx.common.shape_calculator import calculate_linear_classifier_output_shapes  # noqa
import onnxmltools
from onnxmltools.convert.xgboost.operator_converters.XGBoost import convert_xgboost  # noqa
import onnxmltools.convert.common.data_types

data = load_iris()
X =[:, :2]
y =

ind = numpy.arange(X.shape[0])
X = X[ind, :].copy()
y = y[ind].copy()

pipe = Pipeline([('scaler', StandardScaler()),
                 ('lgbm', XGBClassifier(n_estimators=3))]), y)

# The conversion fails but it is expected.

    convert_sklearn(pipe, 'pipeline_xgboost',
                    [('input', FloatTensorType([None, 2]))],
                    target_opset={'': 12, '': 2})
except Exception as e:

# The error message tells no converter was found
# for XGBoost models. By default, *sklearn-onnx*
# only handles models from *scikit-learn* but it can
# be extended to every model following *scikit-learn*
# API as long as the module knows there exists a converter
# for every model used in a pipeline. That's why
# we need to register a converter.


C:\Python395_x64\lib\site-packages\xgboost\ UserWarning: The use of label encoder in XGBClassifier is deprecated and will be removed in a future release. To remove this warning, do the following: 1) Pass option use_label_encoder=False when constructing XGBClassifier object; and 2) Encode your labels (y) as integers starting with 0, i.e. 0, 1, 2, ..., [num_class - 1].
  warnings.warn(label_encoder_deprecation_msg, UserWarning)
[18:40:26] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/ Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'multi:softprob' was changed from 'merror' to 'mlogloss'. Explicitly set eval_metric if you'd like to restore the old behavior.
Unable to find a shape calculator for type '<class 'xgboost.sklearn.XGBClassifier'>'.
It usually means the pipeline being converted contains a
transformer or a predictor with no corresponding converter
implemented in sklearn-onnx. If the converted is implemented
in another library, you need to register
the converted so that it can be used by sklearn-onnx (function
update_registered_converter). If the model is not yet covered
by sklearn-onnx, you may raise an issue to
to get the converter implemented or even contribute to the
project. If the model is a custom model, a new converter must
be implemented. Examples can be found in the gallery.

Register the converter for XGBClassifier

The converter is implemented in onnxmltools: onnxmltools… and the shape calculator: onnxmltools…

Then we import the converter and shape calculator.

Let’s register the new converter.

    XGBClassifier, 'XGBoostXGBClassifier',
    calculate_linear_classifier_output_shapes, convert_xgboost,
    options={'nocl': [True, False], 'zipmap': [True, False, 'columns']})

Convert again

model_onnx = convert_sklearn(
    pipe, 'pipeline_xgboost',
    [('input', FloatTensorType([None, 2]))],
    target_opset={'': 12, '': 2})

# And save.
with open("pipeline_xgboost.onnx", "wb") as f:

Compare the predictions

Predictions with XGBoost.

print("predict", pipe.predict(X[:5]))
print("predict_proba", pipe.predict_proba(X[:1]))


predict [1 1 0 2 1]
predict_proba [[0.14671634 0.48657113 0.36671248]]

Predictions with onnxruntime.

sess = rt.InferenceSession("pipeline_xgboost.onnx")
pred_onx =, {"input": X[:5].astype(numpy.float32)})
print("predict", pred_onx[0])
print("predict_proba", pred_onx[1][:1])


predict [1 1 0 2 1]
predict_proba [{0: 0.14671637117862701, 1: 0.48657119274139404, 2: 0.3667125403881073}]

Display the ONNX graph

pydot_graph = GetPydotGraph(
    model_onnx.graph,, rankdir="TB",
        "docstring", color="yellow",
        fillcolor="yellow", style="filled"))

os.system('dot -O -Gdpi=300 -Tpng')

image = plt.imread("")
fig, ax = plt.subplots(figsize=(40, 20))
plot pipeline xgboost


(-0.5, 2112.5, 2558.5, -0.5)

Versions used for this example

print("numpy:", numpy.__version__)
print("scikit-learn:", sklearn.__version__)
print("onnx: ", onnx.__version__)
print("onnxruntime: ", rt.__version__)
print("skl2onnx: ", skl2onnx.__version__)
print("onnxmltools: ", onnxmltools.__version__)
print("xgboost: ", xgboost.__version__)


numpy: 1.22.1
scikit-learn: 1.1.dev0
onnx:  1.11.0
onnxruntime:  1.11.0+cpu
skl2onnx:  1.11
onnxmltools:  1.11.0
xgboost:  1.5.2

Total running time of the script: ( 0 minutes 3.584 seconds)

Gallery generated by Sphinx-Gallery