Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Microsoft Onnxruntime Convert Sklearn

From Leeroopedia


Metadata

Field Value
Implementation Name Convert_Sklearn
Repository Microsoft_Onnxruntime
Source Repository https://github.com/microsoft/onnxruntime
Type Wrapper Doc
Wrapper Tool skl2onnx
Language Python
Domain ML_Inference, Model_Conversion
Last Updated 2026-02-10
Workflow Train_Convert_Predict
Pair 3 of 5

Overview

Wrapper documentation for the skl2onnx.convert_sklearn() function, which transforms a trained scikit-learn model into ONNX format for inference with ONNX Runtime.

API Signature

skl2onnx.convert_sklearn(model, initial_types=initial_type) -> onnx.ModelProto

Import

from skl2onnx import convert_sklearn

Code Reference

Reference Location
Usage example docs/python/examples/plot_train_convert_predict.py:L56-58

I/O Contract

Inputs

Parameter Type Required Description
model scikit-learn estimator Yes A trained (fitted) scikit-learn model or pipeline.
initial_types list[tuple[str, FloatTensorType]] Yes Input schema definition mapping tensor names to typed tensor descriptors.

Outputs

Output Type Description
return value onnx.ModelProto The converted ONNX model. Call .SerializeToString() to serialize to bytes for saving to disk.

Usage Example

from skl2onnx import convert_sklearn
from skl2onnx.common.data_types import FloatTensorType

# Define input schema
initial_type = [("float_input", FloatTensorType([None, 4]))]

# Convert trained model to ONNX
onx = convert_sklearn(clr, initial_types=initial_type)

# Save to disk
with open("model.onnx", "wb") as f:
    f.write(onx.SerializeToString())

From the source at docs/python/examples/plot_train_convert_predict.py:L56-58:

initial_type = [("float_input", FloatTensorType([None, 4]))]
onx = convert_sklearn(clr, initial_types=initial_type)
with open("logreg_iris.onnx", "wb") as f:
    f.write(onx.SerializeToString())

RandomForest Conversion

From docs/python/examples/plot_train_convert_predict.py:L183-186:

initial_type = [("float_input", FloatTensorType([1, 4]))]
onx = convert_sklearn(rf, initial_types=initial_type)
with open("rf_iris.onnx", "wb") as f:
    f.write(onx.SerializeToString())

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment