Skip to main content

Convert scikit-learn models to ONNX

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

skl2onnx-1.16.0.tar.gz (925.9 kB view details)

Uploaded Source

Built Distribution

skl2onnx-1.16.0-py2.py3-none-any.whl (298.5 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file skl2onnx-1.16.0.tar.gz.

File metadata

  • Download URL: skl2onnx-1.16.0.tar.gz
  • Upload date:
  • Size: 925.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.11

File hashes

Hashes for skl2onnx-1.16.0.tar.gz
Algorithm Hash digest
SHA256 3370b3d4065ce2dc5933878c3273f4aea41f945cc6514543b13ad2d57f369ce5
MD5 8277cdc3d66af7d0e339377febc4ca6e
BLAKE2b-256 e93ff624c1d30324597c419006a826ad095345c7457f75a2e23a65cfb2118cb1

See more details on using hashes here.

File details

Details for the file skl2onnx-1.16.0-py2.py3-none-any.whl.

File metadata

  • Download URL: skl2onnx-1.16.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 298.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.11

File hashes

Hashes for skl2onnx-1.16.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7de548580c625bfa5893fe79c9dd3213c3720b12e1ff8e3fd28967da0698242d
MD5 62d2a9c1aab364d11d925bd25cd61cb7
BLAKE2b-256 2680836824c62ff0923b4c3b8af8332170bdc3ccb469a220535b40405a93b4fb

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page