Skip to content

About

Transpile trained scikit-learn estimators and transformers to C, C#, Go, Java, JavaScript, PHP and Ruby.

Topics

Resources

Contributing

Stars

1.3k stars

Watchers

30 watching

Forks

Repository files navigation

sklearn-porter

Transpile trained scikit-learn estimators and transformers
to C, C#, Go, Java, JavaScript, PHP and Ruby.

CI PyPI version Python versions Downloads License

Installation • Quick Start • Examples • Support • Documentation • Contributing • Citation

Use the generated code to make predictions where Python isn't available or is too heavy, e.g. on embedded systems, in web browsers or in existing applications. The ported estimators make the same predictions as the original scikit-learn estimators, the ported transformers (e.g. for the preprocessing) transform the features like the original transformers.

Installation

uv add sklearn-porter
pip install sklearn-porter

Python 3.11 or newer and scikit-learn 1.8 or newer are required.

Quick Start

from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from sklearn_porter import port, save, test

# Train a classifier:
X, y = load_iris(return_X_y=True)
clf = DecisionTreeClassifier(max_depth=2, random_state=0).fit(X, y)

# Get the source code as string:
# source = port(clf, language='js')

# Save it as JavaScript file:
save(clf, language='js', directory='/tmp')

# Check that it predicts the same classes as scikit-learn (with Node.js):
print(test(clf, X, language='js'))  # 1.0

Run the saved classifier with the features of a sample:

$ node /tmp/DecisionTreeClassifier.js 6.3 3.3 6.0 2.5
Predicted class: 2
Probability of class #0 : 0
Probability of class #1 : 0.021739130434782608
Probability of class #2 : 0.9782608695652174

make() returns the predictions of the ported estimator. make() and test() need the compiler or interpreter of the language (see languages).

Examples

  • ✨ Getting Started: port a decision tree to JavaScript, run it with Node.js and check its predictions.
  • Transformers: port the preprocessing, e.g. a StandardScaler or a ColumnTransformer.
  • Pipelines: port the preprocessing and the classifier of a Pipeline to one source file.
  • CLI: port a saved classifier with the command porter, save the source code to files and run it.

Support

Estimators

Classifiers (21)
Regressors (21)
Outlier detectors (2)

Transformers

Composition (2)
Feature extraction (4)
Feature selection (11)
Preprocessing (15)

¹ without the template combined, ² without predict_proba (even if scikit-learn provides it, e.g. SVC(probability=True)). All other estimators support all languages and templates, the classifiers also predict_proba (if scikit-learn provides it). porter show lists all supported combinations.

Languages

Language Key make() and test() need Tested with
C c gcc GCC 14
C# cs dotnet (.NET SDK 8+) .NET SDK 10
Go go go Go 1.27
Java java javac and java OpenJDK 21, Gson 2.15
JavaScript js node Node.js 24
PHP php php PHP 8.4
Ruby ruby ruby Ruby 3.3

The key selects the language, e.g. port(clf, language='cs').

Templates

The template defines how the model data is stored in the generated code, e.g. port(clf, template='exported'):

  • attached: The model data is embedded in the source code, e.g. as arrays.
  • exported: The model data is saved in a separate JSON file, which the source code reads at runtime.
  • combined: The model data is compiled into the logic of the source code, e.g. as nested if-else statements.

Documentation

Read these pages before you use a ported estimator in production:

  • Usage: How to use the generated classes in your own code, e.g. in a Java or Go program or in a browser.
  • Estimator notes: How each estimator and transformer is ported and which parameters can lead to different predictions.
  • Limitations: The known limitations, e.g. of pipelines, large Java methods, C#, sparse data and Windows.
  • Changelog: All changes of each version.

Contributing

Contributions are welcome! See CONTRIBUTING.md for the development setup, the tests and the release process.

Citation

If you use sklearn-porter in your work, please cite it: GitHub shows the citation of the citation file under "Cite this repository" (APA and BibTeX).

License

sklearn-porter is open source software released under the BSD 3-Clause license.

About

Transpile trained scikit-learn estimators and transformers to C, C#, Go, Java, JavaScript, PHP and Ruby.

Topics

Resources

Contributing

Stars

1.3k stars

Watchers

30 watching

Forks

Releases

Packages

Used by

Contributors

Languages