Skip to content

onnx/sklearn-onnx

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

f94baf9 · Oct 19, 2023
Oct 19, 2023
Aug 1, 2023
Aug 1, 2023
Oct 19, 2023
Oct 19, 2023
Oct 19, 2023
Aug 1, 2023
Aug 30, 2023
Oct 3, 2023
Apr 1, 2021
Dec 13, 2021
Jul 7, 2023
Aug 30, 2023
Aug 1, 2023
Oct 19, 2023
Jul 7, 2023
Apr 1, 2021
Aug 1, 2023

Repository files navigation

Build Status

Build Status

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 15.

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.

License

Apache License v2.0