Skip to content
Projects

nok/sklearn-porter

View on GitHub

nok/sklearn-porter has 1,304 GitHub stars and 168 forks. GitHot measured 0 stars in 24 hours. Last checked 2026-11-09 06:53 UTC. Last push 2026-11-01.

How these counts are measured

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

Measured star growth

in 24h
0
in 7 days
Collecting history
in 15 days
Collecting history
in 1 month
Collecting history

Net star changes over overlapping periods, not daily totals. Missing measurements are left blank. Stars measure attention, not software quality.

README.md

Original

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 make, save, test

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

# Save it to JavaScript files (the code and the model data):
src_path, json_path = save(clf, language='js', template='exported', directory='/tmp')

# Make predictions with the ported classifier (with Node.js):
y_pred, y_proba = make(clf, X[:3], language='js')
print(y_pred)  # [0 0 0]

# Check that all predictions match those of scikit-learn:
print(test(clf, X, language='js'))  # 1.0

Run the saved classifier with the model data and the features of a sample:

$ node /tmp/DecisionTreeClassifier.js /tmp/DecisionTreeClassifier.json 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() and test() need the compiler or interpreter of the language (see languages).

Examples

  • ✨ Getting Started: port a classifier, make predictions and measure the accuracy.
  • Transformers: port transformers (e.g. a StandardScaler) and use the ported transformers.
  • Pipelines: port a Pipeline of transformers and a final estimator 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:

  • 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.

The template is selected like the language, e.g. port(clf, template='exported').

Documentation

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

  • 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 (with the version) under "Cite this repository", or use this BibTeX entry:

@software{sklearn_porter,
  author = {Darius Morawiec},
  title = {sklearn-porter},
  note = {Transpile trained scikit-learn estimators and transformers to C, C#, Go, Java, JavaScript, PHP and Ruby},
  url = {https://github.com/nok/sklearn-porter}
}

License

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

Explore related projects