Transpile trained scikit-learn estimators and transformers
to C, C#, Go, Java, JavaScript, PHP and Ruby.
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.
uv add sklearn-porterpip install sklearn-porterPython 3.11 or newer and scikit-learn 1.8 or newer are required.
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.0Run 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.9782608695652174make() returns the predictions of the ported estimator. make() and test() need the compiler or interpreter of the language (see languages).
- ✨ Getting Started: port a decision tree to JavaScript, run it with Node.js and check its predictions.
- Transformers: port the preprocessing, e.g. a
StandardScaleror aColumnTransformer. - Pipelines: port the preprocessing and the classifier of a
Pipelineto one source file. - CLI: port a saved classifier with the command
porter, save the source code to files and run it.
Classifiers (21)
- ensemble.AdaBoostClassifier
- ensemble.ExtraTreesClassifier
- ensemble.GradientBoostingClassifier
- ensemble.HistGradientBoostingClassifier
- ensemble.RandomForestClassifier
- linear_model.LogisticRegression
- linear_model.LogisticRegressionCV
- linear_model.Perceptron
- linear_model.RidgeClassifier
- linear_model.RidgeClassifierCV
- linear_model.SGDClassifier ²
- naive_bayes.BernoulliNB ¹
- naive_bayes.ComplementNB ¹
- naive_bayes.GaussianNB ¹
- naive_bayes.MultinomialNB ¹
- neighbors.KNeighborsClassifier ¹
- neural_network.MLPClassifier ¹
- svm.LinearSVC ¹
- svm.NuSVC ¹ ²
- svm.SVC ¹ ²
- tree.DecisionTreeClassifier
Regressors (21)
- ensemble.ExtraTreesRegressor
- ensemble.GradientBoostingRegressor
- ensemble.HistGradientBoostingRegressor
- ensemble.RandomForestRegressor
- linear_model.ARDRegression
- linear_model.BayesianRidge
- linear_model.ElasticNet
- linear_model.ElasticNetCV
- linear_model.HuberRegressor
- linear_model.Lasso
- linear_model.LassoCV
- linear_model.LinearRegression
- linear_model.Ridge
- linear_model.RidgeCV
- linear_model.SGDRegressor
- neighbors.KNeighborsRegressor ¹
- neural_network.MLPRegressor ¹
- svm.LinearSVR
- svm.NuSVR ¹
- svm.SVR ¹
- tree.DecisionTreeRegressor
Outlier detectors (2)
Composition (2)
Feature extraction (4)
Feature selection (11)
- feature_selection.GenericUnivariateSelect
- feature_selection.RFE
- feature_selection.RFECV
- feature_selection.SelectFdr
- feature_selection.SelectFpr
- feature_selection.SelectFromModel
- feature_selection.SelectFwe
- feature_selection.SelectKBest
- feature_selection.SelectPercentile
- feature_selection.SequentialFeatureSelector
- feature_selection.VarianceThreshold
Preprocessing (15)
- impute.SimpleImputer
- preprocessing.Binarizer
- preprocessing.FunctionTransformer
- preprocessing.KBinsDiscretizer
- preprocessing.MaxAbsScaler
- preprocessing.MinMaxScaler
- preprocessing.Normalizer
- preprocessing.OneHotEncoder
- preprocessing.OrdinalEncoder
- preprocessing.PolynomialFeatures
- preprocessing.PowerTransformer
- preprocessing.QuantileTransformer
- preprocessing.RobustScaler
- preprocessing.StandardScaler
- preprocessing.TargetEncoder
¹ 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.
| 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').
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.
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.
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: GitHub shows the citation of the citation file under "Cite this repository" (APA and BibTeX).
sklearn-porter is open source software released under the BSD 3-Clause license.