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 ```bash uv add sklearn-porter ``` ```bash pip install sklearn-porter ``` Python 3.11 or newer and scikit-learn 1.8 or newer are required. ## Quick Start ```python 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: ```console $ 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](#languages)). ## Examples - ✨ [Getting Started](https://github.com/nok/sklearn-porter/blob/main/examples/getting_started.ipynb): port a decision tree to JavaScript, run it with Node.js and check its predictions. - [Transformers](https://github.com/nok/sklearn-porter/blob/main/examples/transformers.ipynb): port the preprocessing, e.g. a `StandardScaler` or a `ColumnTransformer`. - [Pipelines](https://github.com/nok/sklearn-porter/blob/main/examples/pipelines.ipynb): port the preprocessing and the classifier of a `Pipeline` to one source file. - [CLI](https://github.com/nok/sklearn-porter/blob/main/examples/cli.ipynb): port a saved classifier with the command `porter`, save the source code to files and run it. ## Support ### Estimators
Classifiers (21) - [ensemble.AdaBoostClassifier](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.AdaBoostClassifier.html) - [ensemble.ExtraTreesClassifier](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.ExtraTreesClassifier.html) - [ensemble.GradientBoostingClassifier](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.GradientBoostingClassifier.html) - [ensemble.HistGradientBoostingClassifier](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.HistGradientBoostingClassifier.html) - [ensemble.RandomForestClassifier](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html) - [linear_model.LogisticRegression](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html) - [linear_model.LogisticRegressionCV](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegressionCV.html) - [linear_model.Perceptron](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.Perceptron.html) - [linear_model.RidgeClassifier](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.RidgeClassifier.html) - [linear_model.RidgeClassifierCV](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.RidgeClassifierCV.html) - [linear_model.SGDClassifier](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.SGDClassifier.html) ² - [naive_bayes.BernoulliNB](https://scikit-learn.org/stable/modules/generated/sklearn.naive_bayes.BernoulliNB.html) ¹ - [naive_bayes.ComplementNB](https://scikit-learn.org/stable/modules/generated/sklearn.naive_bayes.ComplementNB.html) ¹ - [naive_bayes.GaussianNB](https://scikit-learn.org/stable/modules/generated/sklearn.naive_bayes.GaussianNB.html) ¹ - [naive_bayes.MultinomialNB](https://scikit-learn.org/stable/modules/generated/sklearn.naive_bayes.MultinomialNB.html) ¹ - [neighbors.KNeighborsClassifier](https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KNeighborsClassifier.html) ¹ - [neural_network.MLPClassifier](https://scikit-learn.org/stable/modules/generated/sklearn.neural_network.MLPClassifier.html) ¹ - [svm.LinearSVC](https://scikit-learn.org/stable/modules/generated/sklearn.svm.LinearSVC.html) ¹ - [svm.NuSVC](https://scikit-learn.org/stable/modules/generated/sklearn.svm.NuSVC.html) ¹ ² - [svm.SVC](https://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.html) ¹ ² - [tree.DecisionTreeClassifier](https://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeClassifier.html)
Regressors (21) - [ensemble.ExtraTreesRegressor](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.ExtraTreesRegressor.html) - [ensemble.GradientBoostingRegressor](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.GradientBoostingRegressor.html) - [ensemble.HistGradientBoostingRegressor](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.HistGradientBoostingRegressor.html) - [ensemble.RandomForestRegressor](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestRegressor.html) - [linear_model.ARDRegression](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.ARDRegression.html) - [linear_model.BayesianRidge](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.BayesianRidge.html) - [linear_model.ElasticNet](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.ElasticNet.html) - [linear_model.ElasticNetCV](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.ElasticNetCV.html) - [linear_model.HuberRegressor](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.HuberRegressor.html) - [linear_model.Lasso](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.Lasso.html) - [linear_model.LassoCV](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LassoCV.html) - [linear_model.LinearRegression](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html) - [linear_model.Ridge](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.Ridge.html) - [linear_model.RidgeCV](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.RidgeCV.html) - [linear_model.SGDRegressor](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.SGDRegressor.html) - [neighbors.KNeighborsRegressor](https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KNeighborsRegressor.html) ¹ - [neural_network.MLPRegressor](https://scikit-learn.org/stable/modules/generated/sklearn.neural_network.MLPRegressor.html) ¹ - [svm.LinearSVR](https://scikit-learn.org/stable/modules/generated/sklearn.svm.LinearSVR.html) - [svm.NuSVR](https://scikit-learn.org/stable/modules/generated/sklearn.svm.NuSVR.html) ¹ - [svm.SVR](https://scikit-learn.org/stable/modules/generated/sklearn.svm.SVR.html) ¹ - [tree.DecisionTreeRegressor](https://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeRegressor.html)
Outlier detectors (2) - [ensemble.IsolationForest](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.IsolationForest.html) - [svm.OneClassSVM](https://scikit-learn.org/stable/modules/generated/sklearn.svm.OneClassSVM.html) ¹
### Transformers
Composition (2) - [compose.ColumnTransformer](https://scikit-learn.org/stable/modules/generated/sklearn.compose.ColumnTransformer.html) - [pipeline.FeatureUnion](https://scikit-learn.org/stable/modules/generated/sklearn.pipeline.FeatureUnion.html)
Feature extraction (4) - [feature_extraction.text.CountVectorizer](https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.CountVectorizer.html) - [feature_extraction.text.HashingVectorizer](https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.HashingVectorizer.html) - [feature_extraction.text.TfidfTransformer](https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.TfidfTransformer.html) - [feature_extraction.text.TfidfVectorizer](https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.TfidfVectorizer.html)
Feature selection (11) - [feature_selection.GenericUnivariateSelect](https://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.GenericUnivariateSelect.html) - [feature_selection.RFE](https://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.RFE.html) - [feature_selection.RFECV](https://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.RFECV.html) - [feature_selection.SelectFdr](https://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.SelectFdr.html) - [feature_selection.SelectFpr](https://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.SelectFpr.html) - [feature_selection.SelectFromModel](https://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.SelectFromModel.html) - [feature_selection.SelectFwe](https://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.SelectFwe.html) - [feature_selection.SelectKBest](https://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.SelectKBest.html) - [feature_selection.SelectPercentile](https://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.SelectPercentile.html) - [feature_selection.SequentialFeatureSelector](https://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.SequentialFeatureSelector.html) - [feature_selection.VarianceThreshold](https://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.VarianceThreshold.html)
Preprocessing (15) - [impute.SimpleImputer](https://scikit-learn.org/stable/modules/generated/sklearn.impute.SimpleImputer.html) - [preprocessing.Binarizer](https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.Binarizer.html) - [preprocessing.FunctionTransformer](https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.FunctionTransformer.html) - [preprocessing.KBinsDiscretizer](https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.KBinsDiscretizer.html) - [preprocessing.MaxAbsScaler](https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.MaxAbsScaler.html) - [preprocessing.MinMaxScaler](https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.MinMaxScaler.html) - [preprocessing.Normalizer](https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.Normalizer.html) - [preprocessing.OneHotEncoder](https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.OneHotEncoder.html) - [preprocessing.OrdinalEncoder](https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.OrdinalEncoder.html) - [preprocessing.PolynomialFeatures](https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.PolynomialFeatures.html) - [preprocessing.PowerTransformer](https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.PowerTransformer.html) - [preprocessing.QuantileTransformer](https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.QuantileTransformer.html) - [preprocessing.RobustScaler](https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.RobustScaler.html) - [preprocessing.StandardScaler](https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.StandardScaler.html) - [preprocessing.TargetEncoder](https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.TargetEncoder.html)
¹ 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](https://github.com/nok/sklearn-porter/blob/main/docs/usage.md): How to use the generated classes in your own code, e.g. in a Java or Go program or in a browser. - [Estimator notes](https://github.com/nok/sklearn-porter/blob/main/docs/estimator-notes.md): How each estimator and transformer is ported and which parameters can lead to different predictions. - [Limitations](https://github.com/nok/sklearn-porter/blob/main/docs/limitations.md): The known limitations, e.g. of pipelines, large Java methods, C#, sparse data and Windows. - [Changelog](https://github.com/nok/sklearn-porter/blob/main/CHANGELOG.md): All changes of each version. ## Contributing Contributions are welcome! See [CONTRIBUTING.md](https://github.com/nok/sklearn-porter/blob/main/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](https://github.com/nok/sklearn-porter/blob/main/CITATION.cff) under "Cite this repository" (APA and BibTeX). ## License sklearn-porter is open source software released under the [BSD 3-Clause](https://github.com/nok/sklearn-porter/blob/main/LICENSE) license.