Repolode
nok

nok/sklearn-porter

A Python tool that transpiles trained scikit-learn models and transformers into C, C#, Go, Java, JavaScript, PHP or Ruby code.

Stars
1,304
Forks
168
Open issues
4
Last commit
2 days ago

What is it?

sklearn-porter takes a trained scikit-learn estimator, transformer or pipeline and generates source code in C, C#, Go, Java, JavaScript, PHP or Ruby. The generated code can make predictions without a Python runtime, which is useful on embedded devices, in browsers or inside existing applications written in another language.

The project supports a broad set of classifiers, regressors, outlier detectors and preprocessing or feature-selection transformers, including tree ensembles, linear models, SVMs and neural networks. Ported estimators are intended to produce the same predictions as the originals, and ported transformers transform features the same way.

Model data can be attached to the code, exported to a separate JSON file, or compiled into the logic as nested if-else statements. The package offers a Python API, a `porter` command-line tool, and a `test` helper that compares ported output against the original model. Installation requires Python 3.11+ and scikit-learn 1.8+.

Key features

  • 01Transpiles classifiers, regressors and outlier detectors to seven languages.
  • 02Ports common preprocessing and feature-selection transformers and pipelines.
  • 03Offers attached, exported and combined templates for storing model data.
  • 04Provides both a Python API and a `porter` command-line interface.
  • 05Includes a test function that compares ported predictions with the original model.

Use cases

  • Running a trained scikit-learn model on an embedded device without Python.
  • Adding a model's predictions to a Java, Go or PHP backend without a Python service.
  • Running a classifier in the browser with JavaScript.

Quick start

  1. 1

    Install the package with uv

    uv add sklearn-porter
  2. 2

    Or install it with pip

    pip install sklearn-porter
  3. 3

    Train a scikit-learn model and port it with port(clf, language='js') in Python

Things to know

  • Requires Python 3.11 or newer and scikit-learn 1.8 or newer.
  • Only supports a fixed list of scikit-learn estimators and transformers.

In collections

Topics

FAQ

What is sklearn-porter?
A Python tool that transpiles trained scikit-learn models and transformers into C, C#, Go, Java, JavaScript, PHP or Ruby code. sklearn-porter takes a trained scikit-learn estimator, transformer or pipeline and generates source code in C, C#, Go, Java, JavaScript, PHP or Ruby. The generated code can make predictions without a Python runtime, which is useful on embedded devices, in browsers or inside existing applications written in another language.
Is sklearn-porter free to use? What license does it have?
sklearn-porter is open source under the BSD-3-Clause license. Check the license terms before using it in commercial products.
Is sklearn-porter still maintained?
Actively maintained. The last commit to sklearn-porter was on Nov 5, 2026.
What can I use sklearn-porter for?
Running a trained scikit-learn model on an embedded device without Python. Adding a model's predictions to a Java, Go or PHP backend without a Python service. Running a classifier in the browser with JavaScript.
How do I install sklearn-porter?
Install the package with uv: uv add sklearn-porter Or install it with pip: pip install sklearn-porter Train a scikit-learn model and port it with port(clf, language='js') in Python
Where can I get sklearn-porter?
The source code and installation instructions are on GitHub: https://github.com/nok/sklearn-porter

Summary generated by AI from the project's README. GitHub stats refresh daily.

Related projects