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 demo

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.2.tar.gz (33.4 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.2-py3-none-any.whl (22.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: skjson-0.1.2.tar.gz
  • Upload date:
  • Size: 33.4 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.2.tar.gz
Algorithm Hash digest
SHA256 aa184104f74b997cdb66ee8a7fe34b4fe7569e7ef1f28794e29b5d034bd3bef8
MD5 387161098d989cc7f40bf184736d9838
BLAKE2b-256 11b4b2707a2356580e93fdfafb3620f2b4b042be948058669663977740f7a0e4

See more details on using hashes here.

Provenance

The following attestation bundles were made for skjson-0.1.2.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.2-py3-none-any.whl.

File metadata

  • Download URL: skjson-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 22.8 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 056e16e14fbc9d22f439744fddfae7cbb4dc638d07eb44cef6f20651157b17e3
MD5 05140fa4919a066cb9c63a675fccf970
BLAKE2b-256 0de59e7894dd31ea7217a40378bf6fb192b52debdc95b41ff7c97e6f9fff01cb

See more details on using hashes here.

Provenance

The following attestation bundles were made for skjson-0.1.2-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