Skip to main content

Convert scikit-learn models to ONNX

Project description

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.

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.19.0.tar.gz (742.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

skl2onnx-1.19.0-py3-none-any.whl (25.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: skl2onnx-1.19.0.tar.gz
  • Upload date:
  • Size: 742.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for skl2onnx-1.19.0.tar.gz
Algorithm Hash digest
SHA256 5cd1c1a66d45e4b60f90744381587af2afe7b8266bbbb9e1dc7c8c5a9bbd3577
MD5 2bcec7159842409ff8d1d2f3fd4d3acf
BLAKE2b-256 569fc381040502104f7a55e3d9750b537d11162b41aa689e3841c81eeef829bf

See more details on using hashes here.

Provenance

The following attestation bundles were made for skl2onnx-1.19.0.tar.gz:

Publisher: release.yml on onnx/sklearn-onnx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file skl2onnx-1.19.0-py3-none-any.whl.

File metadata

  • Download URL: skl2onnx-1.19.0-py3-none-any.whl
  • Upload date:
  • Size: 25.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for skl2onnx-1.19.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fd94f466667177bd124f7b93c3559a614e642616bb7305cc87ec0677324173c7
MD5 350000ac57f2f6d422260cfd37e70e1c
BLAKE2b-256 361081952cc0610b9a57e19ece49a92e7381874592866f85eab7854f849b35b1

See more details on using hashes here.

Provenance

The following attestation bundles were made for skl2onnx-1.19.0-py3-none-any.whl:

Publisher: release.yml on onnx/sklearn-onnx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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