.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_pipeline_lightgbm.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_pipeline_lightgbm.py: .. _example-lightgbm-pipe: Convert a pipeline with a LightGbm model ======================================== .. index:: LightGbm *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 *LightGbm* model. *sklearn-onnx* can convert the whole pipeline as long as it knows the converter associated to a *LGBMClassifier*. Let's see how to do it. Train a LightGBM classifier +++++++++++++++++++++++++++ .. GENERATED FROM PYTHON SOURCE LINES 22-62 .. code-block:: Python import lightgbm import onnxmltools import skl2onnx import onnx import sklearn import matplotlib.pyplot as plt import os from onnx.tools.net_drawer import GetPydotGraph, GetOpNodeProducer import onnxruntime as rt from onnxruntime.capi.onnxruntime_pybind11_state import Fail as OrtFail from skl2onnx import convert_sklearn, update_registered_converter from skl2onnx.common.shape_calculator import ( calculate_linear_classifier_output_shapes, ) from onnxmltools.convert.lightgbm.operator_converters.LightGbm import ( convert_lightgbm, ) import onnxmltools.convert.common.data_types from skl2onnx.common.data_types import FloatTensorType import numpy from sklearn.datasets import load_iris from sklearn.pipeline import Pipeline from sklearn.preprocessing import StandardScaler from lightgbm import LGBMClassifier data = load_iris() X = data.data[:, :2] y = data.target ind = numpy.arange(X.shape[0]) numpy.random.shuffle(ind) X = X[ind, :].copy() y = y[ind].copy() pipe = Pipeline( [("scaler", StandardScaler()), ("lgbm", LGBMClassifier(n_estimators=3))] ) pipe.fit(X, y) .. rst-class:: sphx-glr-script-out .. code-block:: none [LightGBM] [Info] Auto-choosing col-wise multi-threading, the overhead of testing was 0.003623 seconds. You can set `force_col_wise=true` to remove the overhead. [LightGBM] [Info] Total Bins 47 [LightGBM] [Info] Number of data points in the train set: 150, number of used features: 2 [LightGBM] [Info] Start training from score -1.098612 [LightGBM] [Info] Start training from score -1.098612 [LightGBM] [Info] Start training from score -1.098612 [LightGBM] [Warning] No further splits with positive gain, best gain: -inf [LightGBM] [Warning] No further splits with positive gain, best gain: -inf [LightGBM] [Warning] No further splits with positive gain, best gain: -inf [LightGBM] [Warning] No further splits with positive gain, best gain: -inf [LightGBM] [Warning] No further splits with positive gain, best gain: -inf [LightGBM] [Warning] No further splits with positive gain, best gain: -inf [LightGBM] [Warning] No further splits with positive gain, best gain: -inf [LightGBM] [Warning] No further splits with positive gain, best gain: -inf [LightGBM] [Warning] No further splits with positive gain, best gain: -inf .. raw:: html
Pipeline(steps=[('scaler', StandardScaler()),
                    ('lgbm', LGBMClassifier(n_estimators=3))])
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.


.. GENERATED FROM PYTHON SOURCE LINES 63-74 Register the converter for LGBMClassifier +++++++++++++++++++++++++++++++++++++++++ The converter is implemented in *onnxmltools*: `onnxmltools...LightGbm.py `_. and the shape calculator: `onnxmltools...Classifier.py `_. .. GENERATED FROM PYTHON SOURCE LINES 76-77 Then we import the converter and shape calculator. .. GENERATED FROM PYTHON SOURCE LINES 79-80 Let's register the new converter. .. GENERATED FROM PYTHON SOURCE LINES 80-88 .. code-block:: Python update_registered_converter( LGBMClassifier, "LightGbmLGBMClassifier", calculate_linear_classifier_output_shapes, convert_lightgbm, options={"nocl": [True, False], "zipmap": [True, False, "columns"]}, ) .. GENERATED FROM PYTHON SOURCE LINES 89-91 Convert again +++++++++++++ .. GENERATED FROM PYTHON SOURCE LINES 91-103 .. code-block:: Python model_onnx = convert_sklearn( pipe, "pipeline_lightgbm", [("input", FloatTensorType([None, 2]))], target_opset={"": 12, "ai.onnx.ml": 2}, ) # And save. with open("pipeline_lightgbm.onnx", "wb") as f: f.write(model_onnx.SerializeToString()) .. GENERATED FROM PYTHON SOURCE LINES 104-108 Compare the predictions +++++++++++++++++++++++ Predictions with LightGbm. .. GENERATED FROM PYTHON SOURCE LINES 108-112 .. code-block:: Python print("predict", pipe.predict(X[:5])) print("predict_proba", pipe.predict_proba(X[:1])) .. rst-class:: sphx-glr-script-out .. code-block:: none predict [1 2 2 2 2] predict_proba [[0.25335584 0.45934348 0.28730068]] .. GENERATED FROM PYTHON SOURCE LINES 113-114 Predictions with onnxruntime. .. GENERATED FROM PYTHON SOURCE LINES 114-129 .. code-block:: Python try: sess = rt.InferenceSession( "pipeline_lightgbm.onnx", providers=["CPUExecutionProvider"] ) except OrtFail as e: print(e) print("The converter requires onnxmltools>=1.7.0") sess = None if sess is not None: pred_onx = sess.run(None, {"input": X[:5].astype(numpy.float32)}) print("predict", pred_onx[0]) print("predict_proba", pred_onx[1][:1]) .. rst-class:: sphx-glr-script-out .. code-block:: none predict [1 2 2 2 2] predict_proba [{0: 0.25335583090782166, 1: 0.45934349298477173, 2: 0.287300705909729}] .. GENERATED FROM PYTHON SOURCE LINES 130-132 Display the ONNX graph ++++++++++++++++++++++ .. GENERATED FROM PYTHON SOURCE LINES 132-150 .. code-block:: Python pydot_graph = GetPydotGraph( model_onnx.graph, name=model_onnx.graph.name, rankdir="TB", node_producer=GetOpNodeProducer( "docstring", color="yellow", fillcolor="yellow", style="filled" ), ) pydot_graph.write_dot("pipeline.dot") os.system("dot -O -Gdpi=300 -Tpng pipeline.dot") image = plt.imread("pipeline.dot.png") fig, ax = plt.subplots(figsize=(40, 20)) ax.imshow(image) ax.axis("off") .. image-sg:: /auto_examples/images/sphx_glr_plot_pipeline_lightgbm_001.png :alt: plot pipeline lightgbm :srcset: /auto_examples/images/sphx_glr_plot_pipeline_lightgbm_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none (np.float64(-0.5), np.float64(2549.5), np.float64(2558.5), np.float64(-0.5)) .. GENERATED FROM PYTHON SOURCE LINES 151-152 **Versions used for this example** .. GENERATED FROM PYTHON SOURCE LINES 152-160 .. code-block:: Python 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("lightgbm: ", lightgbm.__version__) .. rst-class:: sphx-glr-script-out .. code-block:: none numpy: 2.2.0 scikit-learn: 1.6.0 onnx: 1.18.0 onnxruntime: 1.21.0+cu126 skl2onnx: 1.18.0 onnxmltools: 1.13.0 lightgbm: 4.5.0 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.351 seconds) .. _sphx_glr_download_auto_examples_plot_pipeline_lightgbm.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_pipeline_lightgbm.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_pipeline_lightgbm.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_pipeline_lightgbm.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_