AI Decision System
Replayable, offline-verifiable records for AI-assisted go/no-go decisions: experiment promotions, typed model answers and human-approved actions.
Transpile trained scikit-learn estimators to C, Java, JavaScript and others.
Translation pending
Developer toolsPythonGitHub @nokSelf-installOwner unconfirmed
Paste into Claude or ChatGPT for installation instructions suited to your environment.
Have you tried it? One tap is enough. No sign-in needed.
The maker's introduction consists of the line above. Read the repository README for more details. Repository README ↗
Loading the full README.
Read the full README ↗Transpile trained scikit-learn estimators and transformers to C, C#, Go, Java, JavaScript, PHP and Ruby.
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.
Navigation: Installation • Support • Usage • CLI • Documentation • Contributing • Citation • License
uv add sklearn-porter
pip install sklearn-porter
Python 3.11 or newer and scikit-learn 1.8 or newer are required.
The estimators and transformers are ported to C (c), C# (cs), Go (go), Java (java), JavaScript (js), PHP (php) and Ruby (ruby). The keys in parentheses select the language, e.g. port(clf, language='cs').
The template defines how the model data is stored in the generated code:
attached: the model data is embedded in the code.exported: the model data is saved in a separate JSON file.combined: the model data is compiled into the logic of the code (e.g. nested if-else statements).from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from sklearn_porter import make, port, save, test
# 1. Load the data and train a classifier:
X, y = load_iris(return_X_y=True)
clf = DecisionTreeClassifier()
clf.fit(X, y)
# 2. Port (transpile) the classifier and get the source code:
output = port(clf, language='js', template='attached')
print(output)
# 3. Save the ported classifier to files:
src_path, json_path = save(clf, language='js', template='exported', directory='/tmp')
print(src_path, json_path)
# 4. Make predictions with the ported classifier:
y_classes, y_probas = make(clf, X[:10], language='js', template='exported')
print(y_classes, y_probas)
# 5. Check the ported classifier: the score is the fraction of predictions
# that match those of the original classifier.
score = test(clf, X[:10], language='js', template='exported')
print(score)
Steps 4 and 5 require a compiler or interpreter for the target language (e.g. node for JavaScript or the .NET SDK for C#). The functions accept transformers and pipelines too, the class Porter provides the same object-oriented API.
The example notebooks show the usage step by step:
StandardScaler or a ColumnTransformer) and use the ported transformers.Pipeline of transformers and a final estimator (also of a GridSearchCV) to one source file.After installation, the porter command is available:
porter {show,port,save} [-h] [-v]
porter show [-l {c,cs,go,java,js,php,ruby}] [-h]
porter port <estimator> [-l {c,cs,go,java,js,php,ruby}]
[-t {attached,combined,exported}]
[--ignore-preprocessing]
[--skip-warnings] [-h]
porter save <estimator> [-l {c,cs,go,java,js,php,ruby}]
[-t {attached,combined,exported}]
[--directory DIRECTORY]
[--ignore-preprocessing]
[--skip-warnings] [-h]
First, save the trained estimator to a file (see model persistence):
from joblib import dump
dump(clf, 'estimator.joblib')
Then port the saved estimator (or transformer or pipeline) with the port subcommand:
porter port estimator.joblib -l js -t attached > estimator.js
The output can be passed to other tools, e.g. to terser, to minify the code:
porter port estimator.joblib -l js -t attached | npx terser --compress -o estimator.min.js
Read these pages before you use a ported estimator in production:
Contributions are welcome! See CONTRIBUTING.md for the development setup, the tests and the release process.
If you use sklearn-porter in your work, please cite it. You can use this BibTeX entry:
@misc{sklearn_porter,
author = {Darius Morawiec},
title = {sklearn-porter},
note = {Transpile trained scikit-learn estimators to C, C#, Go, Java, JavaScript, PHP and Ruby},
url = {https://github.com/nok/sklearn-porter}
}
sklearn-porter is open source software released under the BSD 3-Clause license.