Convert scikit-learn models to ONNX format for cross-platform inference and deployment
npx @tessl/cli install tessl/pypi-skl2onnx@1.19.0skl2onnx provides a comprehensive conversion library that transforms scikit-learn machine learning models into the ONNX (Open Neural Network Exchange) format, enabling high-performance inference and cross-platform deployment. The library supports a wide range of scikit-learn models including classifiers, regressors, transformers, and preprocessing components, with automatic shape inference and type conversion to ensure compatibility with ONNX runtime environments.
pip install skl2onnximport skl2onnxCommon for model conversion:
from skl2onnx import convert_sklearn, to_onnx
from skl2onnx.common.data_types import FloatTensorTypefrom sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification
from skl2onnx import to_onnx
from skl2onnx.common.data_types import FloatTensorType
import numpy as np
# Create and train a model
X, y = make_classification(n_samples=100, n_features=4, random_state=42)
model = RandomForestClassifier(n_estimators=10, random_state=42)
model.fit(X, y)
# Convert to ONNX with automatic type inference
onnx_model = to_onnx(model, X)
# Or specify types explicitly
initial_type = [('float_input', FloatTensorType([None, 4]))]
onnx_model = convert_sklearn(model, initial_types=initial_type)
# Save the ONNX model
with open("model.onnx", "wb") as f:
f.write(onnx_model.SerializeToString())The skl2onnx conversion system is built around several key components:
This modular design enables support for 100+ sklearn model types while maintaining extensibility for custom operators and third-party model types.
Primary functions for converting scikit-learn models to ONNX format, including the main conversion engine, simplified conversion with type inference, and mixin enhancement for sklearn models.
def convert_sklearn(model, name=None, initial_types=None, doc_string="",
target_opset=None, custom_conversion_functions=None,
custom_shape_calculators=None, custom_parsers=None,
options=None, intermediate=False, white_op=None,
black_op=None, final_types=None, dtype=None,
naming=None, model_optim=True, verbose=0): ...
def to_onnx(model, X=None, name=None, initial_types=None, target_opset=None,
options=None, white_op=None, black_op=None, final_types=None,
dtype=None, naming=None, model_optim=True, verbose=0): ...
def wrap_as_onnx_mixin(model, target_opset=None): ...Comprehensive type system for ONNX conversion including tensor types, scalar types, sequence types, and automatic type inference utilities. Supports all major numpy and ONNX data types with shape validation.
class FloatTensorType: ...
class Int64TensorType: ...
class StringTensorType: ...
class BooleanTensorType: ...
def guess_data_type(data_type): ...
def guess_initial_types(X, initial_types=None): ...System for registering custom converters, parsers, and operators. Includes discovery of supported models and extensibility framework for third-party model types.
def update_registered_converter(model, alias=None, shape_fct=None,
convert_fct=None, overwrite=False,
parser=None, options=None): ...
def update_registered_parser(model, parser_fct=None, overwrite=False): ...
def supported_converters(from_sklearn=False): ...
def get_model_alias(model_type): ...Investigation and integration utilities for debugging conversions, comparing outputs between sklearn and ONNX models, and integrating custom ONNX graphs.
def collect_intermediate_steps(model, X=None, target_opset=None): ...
def compare_objects(sklearn_output, onnx_output, decimal=5): ...
def enumerate_pipeline_models(model): ...
def add_onnx_graph(onx, to_add, inputs, outputs): ...ONNX operator creation system and mixin classes for enhancing scikit-learn models with ONNX capabilities. Enables direct ONNX operator composition and sklearn integration.
class OnnxOperator: ...
class OnnxOperatorMixin: ...The library provides extensive support for scikit-learn models:
Complete model support list available in the registration documentation.
__version__ = "1.19.1"
__max_supported_opset__ = 21
def get_latest_tested_opset_version(): ...The library raises specific exceptions for common conversion issues:
MissingConverter - When no converter is registered for a model typeMissingShapeCalculator - When shape inference fails for an operator