Skip to main content

Export scikit-learn models to JSON for cross-language inference

Project description

skjson

Export scikit-learn models to JSON for cross-language inference.

Related repos

  1. Running inference using npm package: skjson-js
  2. Check out the Info Page

Installation

pip install skjson

Quick Start

Check out the demo notebook for an example training a Random Forest on the Iris dataset:

from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
import skjson

# Load the Iris dataset and split
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train a Random Forest classifier
clf = RandomForestClassifier(random_state=42)
clf.fit(X_train, y_train)

# Export the trained model to JSON
skjson.save(clf, 'demo.json')

# Load and reuse the model
predictor = skjson.load("demo.json")

Convert from Joblib and ONNX

skjson supports converting sklearn models directly from joblib and ONNX format without the need to load the model in Python first.

Convert from Joblib

import skjson

# Convert a joblib-serialized sklearn model to JSON
skjson.joblib_to_json("model.joblib")
# → Creates "model.json" in the same directory

# Or specify a custom output path and feature names
skjson.joblib_to_json(
    "model.joblib",
    output_path="output/model.json",
    feature_names=["sepal_length", "sepal_width", "petal_length", "petal_width"]
)

Convert from ONNX

Works with ONNX models exported from sklearn using skl2onnx:

import skjson

# Convert an ONNX model to JSON
skjson.onnx_to_json("model.onnx")
# → Creates "model.json" in the same directory

# Or specify a custom output path
skjson.onnx_to_json("model.onnx", output_path="output/model.json")

Supported ONNX model types (exported from sklearn via skl2onnx):

ONNX Operator sklearn Model
LinearRegressor LinearRegression, Ridge, Lasso, ElasticNet
LinearClassifier LogisticRegression
TreeEnsembleClassifier DecisionTreeClassifier, RandomForestClassifier
TreeEnsembleRegressor DecisionTreeRegressor, RandomForestRegressor
SVMClassifier SVC, LinearSVC
SVMRegressor SVR

Supported models for conversion

Linear Models (sklearn.linear_model)

  • LinearRegression, Ridge, Lasso, ElasticNet
  • LogisticRegression

Tree-based Models (sklearn.tree, sklearn.ensemble)

  • DecisionTreeClassifier, DecisionTreeRegressor
  • RandomForestClassifier, RandomForestRegressor
  • GradientBoostingClassifier, GradientBoostingRegressor

Support Vector Machines (sklearn.svm)

  • SVC, SVR, LinearSVC

Neighbors (sklearn.neighbors)

  • KNeighborsClassifier, KNeighborsRegressor

Naive Bayes (sklearn.naive_bayes)

  • GaussianNB

Preprocessing (sklearn.preprocessing)

  • StandardScaler, MinMaxScaler, LabelEncoder

Future Directions

  • Support for scikit-learn Pipeline objects and feature unions.
  • npm package for inference using our json models
  • Support for sklearn.neural_network models

Note

  • This project built with Google Gemini and Claude Opus.
  • If you wish to contribute, please contact me.

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

skjson-0.1.3.tar.gz (36.7 kB view details)

Uploaded Source

Built Distribution

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

skjson-0.1.3-py3-none-any.whl (22.9 kB view details)

Uploaded Python 3

File details

Details for the file skjson-0.1.3.tar.gz.

File metadata

  • Download URL: skjson-0.1.3.tar.gz
  • Upload date:
  • Size: 36.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for skjson-0.1.3.tar.gz
Algorithm Hash digest
SHA256 e1f8fa3b17ad1c27f03d8512308a3dd3073a636d898503d83c3f4c296ab04ffa
MD5 36d5ab6684d11d20abe0c64491e5cadd
BLAKE2b-256 2fc342f4858bff67229932392f77d85a263b6eb9720b2f7d4c769b326b8d5bfa

See more details on using hashes here.

Provenance

The following attestation bundles were made for skjson-0.1.3.tar.gz:

Publisher: publish.yml on hongyaok/skjson

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

File details

Details for the file skjson-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: skjson-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 22.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for skjson-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 dc63cd750cbf073e2d2056a1e33fdecadbda9eb0a5f06d84464ded43441f867f
MD5 6b24bc9c2de7b2848e8d2503493f8730
BLAKE2b-256 ea6274ea7948d31dd91c3b834807993df781155ea7bf45e4e60a8f2d7edd9fb1

See more details on using hashes here.

Provenance

The following attestation bundles were made for skjson-0.1.3-py3-none-any.whl:

Publisher: publish.yml on hongyaok/skjson

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