Convert scikit-learn models to ONNX format for cross-platform inference and deployment
npx @tessl/cli install tessl/pypi-skl2onnx@1.19.00
# skl2onnx
1
2
skl2onnx 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.
3
4
## Package Information
5
6
- **Package Name**: skl2onnx
7
- **Language**: Python
8
- **Installation**: `pip install skl2onnx`
9
10
## Core Imports
11
12
```python
13
import skl2onnx
14
```
15
16
Common for model conversion:
17
18
```python
19
from skl2onnx import convert_sklearn, to_onnx
20
from skl2onnx.common.data_types import FloatTensorType
21
```
22
23
## Basic Usage
24
25
```python
26
from sklearn.ensemble import RandomForestClassifier
27
from sklearn.datasets import make_classification
28
from skl2onnx import to_onnx
29
from skl2onnx.common.data_types import FloatTensorType
30
import numpy as np
31
32
# Create and train a model
33
X, y = make_classification(n_samples=100, n_features=4, random_state=42)
34
model = RandomForestClassifier(n_estimators=10, random_state=42)
35
model.fit(X, y)
36
37
# Convert to ONNX with automatic type inference
38
onnx_model = to_onnx(model, X)
39
40
# Or specify types explicitly
41
initial_type = [('float_input', FloatTensorType([None, 4]))]
42
onnx_model = convert_sklearn(model, initial_types=initial_type)
43
44
# Save the ONNX model
45
with open("model.onnx", "wb") as f:
46
f.write(onnx_model.SerializeToString())
47
```
48
49
## Architecture
50
51
The skl2onnx conversion system is built around several key components:
52
53
- **Conversion Engine**: Core functions that orchestrate the transformation from sklearn to ONNX
54
- **Type System**: Comprehensive data type definitions that map Python/NumPy types to ONNX types
55
- **Shape Calculators**: Functions that infer output shapes for each operator during conversion
56
- **Operator Converters**: Implementation modules that convert specific sklearn models to ONNX operators
57
- **Registration System**: Extensible framework for adding custom converters and parsers
58
- **Parser System**: Components that analyze sklearn models to extract conversion-relevant information
59
60
This modular design enables support for 100+ sklearn model types while maintaining extensibility for custom operators and third-party model types.
61
62
## Capabilities
63
64
### Core Conversion Functions
65
66
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.
67
68
```python { .api }
69
def convert_sklearn(model, name=None, initial_types=None, doc_string="",
70
target_opset=None, custom_conversion_functions=None,
71
custom_shape_calculators=None, custom_parsers=None,
72
options=None, intermediate=False, white_op=None,
73
black_op=None, final_types=None, dtype=None,
74
naming=None, model_optim=True, verbose=0): ...
75
76
def to_onnx(model, X=None, name=None, initial_types=None, target_opset=None,
77
options=None, white_op=None, black_op=None, final_types=None,
78
dtype=None, naming=None, model_optim=True, verbose=0): ...
79
80
def wrap_as_onnx_mixin(model, target_opset=None): ...
81
```
82
83
[Core Conversion](./conversion.md)
84
85
### Data Types and Type System
86
87
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.
88
89
```python { .api }
90
class FloatTensorType: ...
91
class Int64TensorType: ...
92
class StringTensorType: ...
93
class BooleanTensorType: ...
94
95
def guess_data_type(data_type): ...
96
def guess_initial_types(X, initial_types=None): ...
97
```
98
99
[Data Types](./data-types.md)
100
101
### Registration and Extensibility
102
103
System for registering custom converters, parsers, and operators. Includes discovery of supported models and extensibility framework for third-party model types.
104
105
```python { .api }
106
def update_registered_converter(model, alias=None, shape_fct=None,
107
convert_fct=None, overwrite=False,
108
parser=None, options=None): ...
109
110
def update_registered_parser(model, parser_fct=None, overwrite=False): ...
111
112
def supported_converters(from_sklearn=False): ...
113
114
def get_model_alias(model_type): ...
115
```
116
117
[Registration](./registration.md)
118
119
### Helper Utilities
120
121
Investigation and integration utilities for debugging conversions, comparing outputs between sklearn and ONNX models, and integrating custom ONNX graphs.
122
123
```python { .api }
124
def collect_intermediate_steps(model, X=None, target_opset=None): ...
125
def compare_objects(sklearn_output, onnx_output, decimal=5): ...
126
def enumerate_pipeline_models(model): ...
127
def add_onnx_graph(onx, to_add, inputs, outputs): ...
128
```
129
130
[Helpers](./helpers.md)
131
132
### Algebra and ONNX Operators
133
134
ONNX operator creation system and mixin classes for enhancing scikit-learn models with ONNX capabilities. Enables direct ONNX operator composition and sklearn integration.
135
136
```python { .api }
137
class OnnxOperator: ...
138
class OnnxOperatorMixin: ...
139
```
140
141
[Algebra](./algebra.md)
142
143
## Supported Models
144
145
The library provides extensive support for scikit-learn models:
146
147
- **60+ Classifiers** - Linear, tree-based, ensemble, neural network, naive Bayes, SVM, neighbors, and meta-classifiers
148
- **40+ Regressors** - Linear, tree-based, ensemble, neural network, SVM, gaussian processes, and specialized regressors
149
- **30+ Transformers** - Scaling, encoding, feature engineering, text processing, imputation, decomposition, feature selection, discretization, and pipelines
150
- **Clustering & Outlier Detection** - KMeans, isolation forest, local outlier factor, gaussian mixtures
151
152
Complete model support list available in the registration documentation.
153
154
## Version Information
155
156
```python { .api }
157
__version__ = "1.19.1"
158
__max_supported_opset__ = 21
159
160
def get_latest_tested_opset_version(): ...
161
```
162
163
## Error Handling
164
165
The library raises specific exceptions for common conversion issues:
166
167
- `MissingConverter` - When no converter is registered for a model type
168
- `MissingShapeCalculator` - When shape inference fails for an operator
169
- Various runtime errors for unsupported operations or configuration conflicts