sklearn-onnx

onnxApache-2.0活跃维护

把 scikit-learn 训练好的模型和 Pipeline 转成 ONNX 格式,方便跨框架部署和推理加速。

Convert scikit-learn models and pipelines to ONNX

1.20.0发布于 2026-01-30 10:51 UTC634star +1 / 7 天135fork同步于 2026-11-10 23:55 UTC

怎么上手

由 README 提炼,执行前请确认
pip install skl2onnx
硬件要求按 README 说明

README 未说明硬件要求;示例用 onnxruntime 的 CPUExecutionProvider 在 CPU 上推理

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

把 scikit-learn 模型和 Pipeline 转成 ONNX,再用 ONNX Runtime 做高性能推理

把 scikit-learn 训练好的模型和 Pipeline 转成 ONNX 格式,方便跨框架部署和推理加速。

核心能力

  • 用 to_onnx 一行把训练好的 sklearn 模型导出为 ONNX
  • 支持 scikit-learn Pipeline 及外部库模型、变换器的转换
  • 所有转换器均用 onnxruntime 验证输出一致性
  • 可注册自定义转换器扩展对外部模型的转换支持
  • 支持到 opset 21 的 ONNX 算子集

容易踩的坑

推理侧需另行安装 onnxruntime,示例依赖它加载 .onnx 文件;输入数据需转成 float32,示例中显式做了 astype(np.float32);提交 PR 前需运行 black . 和 ruff check . 修正代码风格

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

5 个版本1.20.0
  1. 1.20.0最新正式版2026-01-30 · 2026-01-30 10:51 UTC
    • Update CI to scikit-learn==1.8.0 #1219
    • When creating OrdinalExporter, respect infrequent categories attribute #1217
    • Adds sklearn_is_fitted to CastTransformer #1209
    • Fixes missing dimension (number of features) in StackingClassifier #1201
    • Fixes CastTransformer output type #1200
    • Fixes unknown_value=np.nan in OrdinalEncoder #1198
    • Enhance OrdinalEncoder conversion to handle infrequent categories #1195
    • Errors in validating Inputs and Outputs of operators raise dedicated exceptions instead of generic RuntimeError [#1205] (https://github.com/onnx/sklearn-onnx/pull/1205)
  2. v1.19.11.19.12025-05-28 · 2025-05-28 17:36 UTC
    查看更新说明
    • Fix QDA converter crashing on string labels and incorrect shape calculation #1169
    • Remove import of split_complex_to_pairs #1182
    • Removes dependency on onnxconverter-common #1179
    • Implements converter for QuantileTransformer #1164,
    • Refactors KNNImputer converter with local function to match scikit-learn's implementation, the code was partially automatically generated from an equivalent implementation in pytorch and exported into ONNX #1167, #1165
    • Add support to sklearn TargetEncoder #1137
    • Fixes missing WhiteKernel with return_std=True #1163
    • Fix empty column selector #1159
    • Fix conversion for XGBClassifier and XGBRegressor #1157
    • Test SelectKBest + StandardScaler pipeline #1156
    • Fix np.NAN into np.nan, #1148
  3. v1.19.0.11.19.02025-05-28 · 2025-05-28 16:35 UTC
    查看更新说明

    0.19.0 is incomplete on pypi, please use 1.19.1

    • Fix QDA converter crashing on string labels and incorrect shape calculation #1169
    • Remove import of split_complex_to_pairs #1182
    • Removes dependency on onnxconverter-common #1179
    • Implements converter for QuantileTransformer #1164,
    • Refactors KNNImputer converter with local function to match scikit-learn's implementation, the code was partially automatically generated from an equivalent implementation in pytorch and exported into ONNX #1167, #1165
    • Add support to sklearn TargetEncoder #1137
    • Fixes missing WhiteKernel with return_std=True #1163
    • Fix empty column selector #1159
    • Fix conversion for XGBClassifier and XGBRegressor #1157
    • Test SelectKBest + StandardScaler pipeline #1156
    • Fix np.NAN into np.nan, #1148
  4. v1.181.18.02024-12-18 · 2024-12-18 07:32 UTC
    查看更新说明
    • Add converter for TunedThresholdClassifierCV #1107
    • Changes main opset in function to_onnx #1109
    • Avoid concatenation if not needed #1110
    • Update and Fix documentation #1113
    • Support fill_value for SimpleImputer with string data #1123
    • Remove unnecessary options for Regressor #1124
    • OrdinalEncoder handle encoded_missing_value and unknown_value #1132
    • Create output_onnx_single_probability.py #1139 #1141
  5. 1.17.02024-06-03 · 2024-06-03 14:25 UTC
    查看更新说明

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    PyPI - Version Linux Windows/Macos Code style: black

    Introduction

    sklearn-onnx converts scikit-learn models to ONNX. Once in the ONNX format, you can use tools like ONNX Runtime for high performance scoring. All converters are tested with onnxruntime. Any external converter can be registered to convert scikit-learn pipeline including models or transformers coming from external libraries.

    Documentation

    Full documentation including tutorials is available at https://onnx.ai/sklearn-onnx/. Supported scikit-learn Models Last supported opset is 21.

    You may also find answers in existing issues or submit a new one.

    Installation

    You can install from PyPi:

    pip install skl2onnx
    

    Or you can install from the source with the latest changes.

    pip install git+https://github.com/onnx/sklearn-onnx.git
    

    Getting started

    # Train a model.
    import numpy as np
    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 = X.astype(np.float32)
    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 to_onnx
    
    onx = to_onnx(clr, X[:1])
    with open("rf_iris.onnx", "wb") as f:
        f.write(onx.SerializeToString())
    
    # Compute the prediction with onnxruntime.
    import onnxruntime as rt
    
    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(np.float32)})[0]
    

    Contribute

    We welcome contributions in the form of feedback, ideas, or code.

    PR

    Before you submit any PR, you should apply the following command lines to fix the style issues.

    black .
    ruff check .
    

    License

    Apache License v2.0