.. SPDX-License-Identifier: Apache-2.0 sklearn-onnx: Convert your scikit-learn model into ONNX ======================================================= .. list-table: :header-rows: 1 :widths: 5 5 * - Linux - Windows * - .. image:: https://dev.azure.com/onnxmltools/sklearn-onnx/_apis/build/status/sklearn-onnx-linux-conda-ci?branchName=master :target: https://dev.azure.com/onnxmltools/sklearn-onnx/_build/latest?definitionId=5?branchName=master - .. image:: https://dev.azure.com/onnxmltools/sklearn-onnx/_apis/build/status/sklearn-onnx-win32-conda-ci?branchName=master :target: https://dev.azure.com/onnxmltools/sklearn-onnx/_build/latest?definitionId=5?branchName=master *sklearn-onnx* enables you to convert models from `scikit-learn `_ toolkits into `ONNX `_. .. toctree:: :maxdepth: 1 introduction index_tutorial api_summary auto_examples/index pipeline parameterized supported **Issues, questions** You should look for `existing issues `_ or submit a new one. Sources are available on `onnx/sklearn-onnx `_. **ONNX version** .. index:: target_opset, opset version The converter can convert a model for a specific version of ONNX. Every ONNX release is labelled with an opset number returned by function `onnx_opset_version `_. This function returns the default value for parameter target opset (parameter *target_opset*) if it is not specified when converting the model. Every operator is versioned. The library chooses the most recent version below or equal to the targetted opset number for every operator. The ONNX model has one opset number for every operator domain, this value is the maximum opset number among all onnx nodes. .. runpython:: :showcode: from skl2onnx import __max_supported_opset__ print("Last supported opset:", __max_supported_opset__) **Backend** *sklearn-onnx* converts models in ONNX format which can be then used to compute predictions with the backend of your choice. However, there exists a way to automatically check every converter with `onnxruntime `_, `onnxruntime-gpu `_. Every converter is tested with this backend. :: # Train a model. from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.ensemble import RandomForestClassifier iris = load_iris() X, y = iris.data, iris.target X_train, X_test, y_train, y_test = train_test_split(X, y) clr = RandomForestClassifier() clr.fit(X_train, y_train) # Convert into ONNX format from skl2onnx import convert_sklearn from skl2onnx.common.data_types import FloatTensorType initial_type = [('float_input', FloatTensorType([None, 4]))] onx = convert_sklearn(clr, initial_types=initial_type) with open("rf_iris.onnx", "wb") as f: f.write(onx.SerializeToString()) # Compute the prediction with ONNX Runtime import onnxruntime as rt import numpy sess = rt.InferenceSession("rf_iris.onnx", providers=["CPUExecutionProvider"]) input_name = sess.get_inputs()[0].name label_name = sess.get_outputs()[0].name pred_onx = sess.run([label_name], {input_name: X_test.astype(numpy.float32)})[0] **Related converters** *sklearn-onnx* only converts models from *scikit-learn*. `onnxmltools `_ can be used to convert models for *libsvm*, *lightgbm*, *xgboost*. Other converters can be found on `github/onnx `_, `torch.onnx `_, `ONNX-MXNet API `_, `Microsoft.ML.Onnx `_... **Credits** The package was started by the following engineers and data scientists at Microsoft starting from winter 2017: Zeeshan Ahmed, Wei-Sheng Chin, Aidan Crook, Xavier Dupré, Costin Eseanu, Tom Finley, Lixin Gong, Scott Inglis, Pei Jiang, Ivan Matantsev, Prabhat Roy, M. Zeeshan Siddiqui, Shouheng Yi, Shauheen Zahirazami, Yiwen Zhu, Du Li, Xuan Li, Wenbing Li. **License** It is licensed with `Apache License v2.0 <../LICENSE>`_.