.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gyexamples/plot_gexternal_lightgbm.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_gyexamples_plot_gexternal_lightgbm.py: .. _example-lightgbm: Convert a pipeline with a LightGBM classifier ============================================= .. index:: LightGBM :epkg:`sklearn-onnx` only converts :epkg:`scikit-learn` models into *ONNX* but many libraries implement :epkg:`scikit-learn` API so that their models can be included in a :epkg:`scikit-learn` pipeline. This example considers a pipeline including a :epkg:`LightGBM` model. :epkg:`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. .. contents:: :local: Train a LightGBM classifier +++++++++++++++++++++++++++ .. GENERATED FROM PYTHON SOURCE LINES 22-48 .. code-block:: default from pyquickhelper.helpgen.graphviz_helper import plot_graphviz from mlprodict.onnxrt import OnnxInference import onnxruntime as rt from skl2onnx import convert_sklearn, update_registered_converter from skl2onnx.common.shape_calculator import calculate_linear_classifier_output_shapes # noqa from onnxmltools.convert.lightgbm.operator_converters.LightGbm import convert_lightgbm # noqa 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) .. 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 49-60 Register the converter for LGBMClassifier +++++++++++++++++++++++++++++++++++++++++ The converter is implemented in :epkg:`onnxmltools`: `onnxmltools...LightGbm.py `_. and the shape calculator: `onnxmltools...Classifier.py `_. .. GENERATED FROM PYTHON SOURCE LINES 60-66 .. code-block:: default 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 67-69 Convert again +++++++++++++ .. GENERATED FROM PYTHON SOURCE LINES 69-80 .. code-block:: default model_onnx = convert_sklearn( pipe, 'pipeline_lightgbm', [('input', FloatTensorType([None, 2]))], target_opset={'': 14, 'ai.onnx.ml': 2}, options={'lgbm__zipmap': False}) # And save. with open("pipeline_lightgbm.onnx", "wb") as f: f.write(model_onnx.SerializeToString()) .. GENERATED FROM PYTHON SOURCE LINES 81-85 Compare the predictions +++++++++++++++++++++++ Predictions with LightGbm. .. GENERATED FROM PYTHON SOURCE LINES 85-89 .. code-block:: default print("predict", pipe.predict(X[:5])) print("predict_proba", pipe.predict_proba(X[:1])) .. rst-class:: sphx-glr-script-out .. code-block:: none predict [0 1 0 0 0] predict_proba [[0.52589117 0.23688316 0.23722567]] .. GENERATED FROM PYTHON SOURCE LINES 90-91 Predictions with onnxruntime. .. GENERATED FROM PYTHON SOURCE LINES 91-99 .. code-block:: default sess = rt.InferenceSession("pipeline_lightgbm.onnx", providers=['CPUExecutionProvider']) 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 [0 1 0 0 0] predict_proba [[0.5258912 0.23688316 0.23722567]] .. GENERATED FROM PYTHON SOURCE LINES 100-102 Final graph +++++++++++ .. GENERATED FROM PYTHON SOURCE LINES 102-108 .. code-block:: default oinf = OnnxInference(model_onnx) ax = plot_graphviz(oinf.to_dot()) ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) .. image-sg:: /gyexamples/images/sphx_glr_plot_gexternal_lightgbm_001.png :alt: plot gexternal lightgbm :srcset: /gyexamples/images/sphx_glr_plot_gexternal_lightgbm_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 1.275 seconds) .. _sphx_glr_download_gyexamples_plot_gexternal_lightgbm.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_gexternal_lightgbm.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_gexternal_lightgbm.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_