.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_convert_decision_function.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_convert_decision_function.py: .. _l-rf-example-decision-function: Probabilities or raw scores =========================== A classifier usually returns a matrix of probabilities. By default, *sklearn-onnx* creates an ONNX graph which returns probabilities but it may skip that step and return raw scores if the model implements the method *decision_function*. Option ``'raw_scores'`` is used to change the default behaviour. Let's see that on a simple example. Train a model and convert it ++++++++++++++++++++++++++++ .. GENERATED FROM PYTHON SOURCE LINES 22-44 .. code-block:: Python import numpy import sklearn from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split import onnxruntime as rt import onnx import skl2onnx from skl2onnx.common.data_types import FloatTensorType from skl2onnx import convert_sklearn from sklearn.linear_model import LogisticRegression iris = load_iris() X, y = iris.data, iris.target X_train, X_test, y_train, y_test = train_test_split(X, y) clr = LogisticRegression(max_iter=500) clr.fit(X_train, y_train) print(clr) initial_type = [("float_input", FloatTensorType([None, 4]))] onx = convert_sklearn(clr, initial_types=initial_type, target_opset=12) .. rst-class:: sphx-glr-script-out .. code-block:: none LogisticRegression(max_iter=500) .. GENERATED FROM PYTHON SOURCE LINES 45-50 Output type +++++++++++ Let's confirm the output type of the probabilities is a list of dictionaries with onnxruntime. .. GENERATED FROM PYTHON SOURCE LINES 50-56 .. code-block:: Python sess = rt.InferenceSession(onx.SerializeToString(), providers=["CPUExecutionProvider"]) res = sess.run(None, {"float_input": X_test.astype(numpy.float32)}) print("skl", clr.predict_proba(X_test[:1])) print("onnx", res[1][:2]) .. rst-class:: sphx-glr-script-out .. code-block:: none skl [[2.18706981e-05 3.86603824e-02 9.61317747e-01]] onnx [{0: 2.1870704586035572e-05, 1: 0.03866042196750641, 2: 0.9613177180290222}, {0: 1.1326104868203402e-05, 1: 0.06515791267156601, 2: 0.9348307847976685}] .. GENERATED FROM PYTHON SOURCE LINES 57-60 Raw scores and decision_function ++++++++++++++++++++++++++++++++ .. GENERATED FROM PYTHON SOURCE LINES 60-74 .. code-block:: Python initial_type = [("float_input", FloatTensorType([None, 4]))] options = {id(clr): {"raw_scores": True}} onx2 = convert_sklearn( clr, initial_types=initial_type, options=options, target_opset=12 ) sess2 = rt.InferenceSession( onx2.SerializeToString(), providers=["CPUExecutionProvider"] ) res2 = sess2.run(None, {"float_input": X_test.astype(numpy.float32)}) print("skl", clr.decision_function(X_test[:1])) print("onnx", res2[1][:2]) .. rst-class:: sphx-glr-script-out .. code-block:: none skl [[-6.0561118 1.42131109 4.63480072]] onnx [{0: -6.056112289428711, 1: 1.4213112592697144, 2: 4.634799957275391}, {0: -6.659489631652832, 1: 1.997969150543213, 2: 4.661520957946777}] .. GENERATED FROM PYTHON SOURCE LINES 75-76 **Versions used for this example** .. GENERATED FROM PYTHON SOURCE LINES 76-82 .. code-block:: Python print("numpy:", numpy.__version__) print("scikit-learn:", sklearn.__version__) print("onnx: ", onnx.__version__) print("onnxruntime: ", rt.__version__) print("skl2onnx: ", skl2onnx.__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 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.042 seconds) .. _sphx_glr_download_auto_examples_plot_convert_decision_function.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_convert_decision_function.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_convert_decision_function.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_convert_decision_function.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_