skl2onnx is a Python library that enables the conversion of scikit-learn machine learning models and pipelines into the ONNX (Open Neural Network Exchange) format. This conversion allows for improved model portability across different runtimes and often leads to enhanced inference performance, especially with ONNX Runtime. The library is actively maintained with frequent releases, typically on a monthly or bi-monthly cadence, and is currently at version 1.20.0.
pip install skl2onnx onnx onnxruntimeVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to train a simple RandomForestClassifier from scikit-learn, convert it to the ONNX format using `skl2onnx.to_onnx`, save the ONNX model to a file, and then load it with ONNX Runtime for inference. It highlights the typical workflow from training to ONNX-based prediction.
Install `skl2onnx==1.19.1` or a more recent version like `pip install skl2onnx`.
Ensure your scikit-learn installation is up-to-date, preferably `scikit-learn>=1.1`, to maintain compatibility and receive tested support.
Review your custom converter implementations and update them to use `skl2onnx`'s internal utilities or directly depend on `onnxconverter-common` if still needed for other purposes.
Always pass a small sample of your input data (e.g., `X[:1]`) to `to_onnx` for inference, or explicitly define `initial_types` (e.g., `[('input', FloatTensorType([None, n_features]))]`) for `convert_sklearn`.Add `target_opset=N` (e.g., `target_opset=22`) to your `to_onnx` or `convert_sklearn` call, aligning with your ONNX Runtime version's capabilities.
Pass `options={id(model): {'zipmap': False}}` to your `to_onnx` or `convert_sklearn` call to disable ZipMap output for classification models if needed.Consult the `skl2onnx` documentation's 'Supported scikit-learn Models' and 'Advanced scenarios' sections for a comprehensive list of supported models and known limitations, especially regarding sparse inputs or complex transformers.