Package: sklearn-porter
sklearn-porterTranspile trainedscikit-learnestimators to C, Java, JavaScript and others.It's recommended for limited embedded systems and critical applications where performance matters most.ImportantWe're hard working on thefirst major releaseof sklearn-porter.Until that we will just release bugfixes to the stable version.EstimatorsEstimatorProgramming languageClassifierJava *JSCGoPHPRubysvm.SVC✓,✓ ᴵ✓✓✓✓svm.NuSVC✓,✓ ᴵ✓✓✓✓svm.LinearSVC✓,✓ ᴵ✓✓✓✓✓tree.DecisionTreeClassifier✓,✓ ᴱ,✓ ᴵ✓,✓ ᴱ✓,✓ ᴱ✓,✓ ᴱ✓,✓ ᴱ✓,✓ ᴱensemble.RandomForestClassifier✓ ᴱ,✓ ᴵ✓ ᴱ✓ ᴱ✓ ᴱ✓ ᴱ✓ ᴱensemble.ExtraTreesClassifier✓ ᴱ,✓ ᴵ✓ ᴱ✓ ᴱ✓ ᴱ✓ ᴱensemble.AdaBoostClassifier✓ ᴱ,✓ ᴵ✓ ᴱ,✓ ᴵ✓ ᴱneighbors.KNeighborsClassifier✓,✓ ᴵ✓,✓ ᴵnaive_bayes.GaussianNB✓,✓ ᴵ✓naive_bayes.BernoulliNB✓,✓ ᴵ✓neural_network.MLPClassifier✓,✓ ᴵ✓,✓ ᴵRegressorJava *JSCGoPHPRubyneural_network.MLPRegressor✓✓ = is full-featured,　ᴱ = with embedded model data,　ᴵ = with imported model data,　* = default languageInstallationStable$pipinstallsklearn-porterDevelopmentIf you want thelatest changes, you can install this package from themasterbranch:$pipuninstall-ysklearn-porter
$pipinstall--no-cache-dirhttps://github.com/nok/sklearn-porter/zipball/masterUsageExportThe following example demonstrates how you can transpile adecision tree estimatorto Java:fromsklearn.datasetsimportload_irisfromsklearn.treeimporttreefromsklearn_porterimportPorter# Load data and train the classifier:samples=load_iris()X,y=samples.data,samples.targetclf=tree.DecisionTreeClassifier()clf.fit(X,y)# Export:porter=Porter(clf,language='java')output=porter.export(embed_data=True)print(output)The exportedresultmatches theofficial human-readable versionof the decision tree.IntegrityYou should always check and compute the integrity between the original and the transpiled estimator:# ...porter=Porter(clf,language='java')# Compute integrity score:integrity=porter.integrity_score(X)print(integrity)# 1.0PredictionYou can compute the prediction(s) in the target programming language:# ...porter=Porter(clf,language='java')# Prediction(s):Y_java=porter.predict(X)y_java=porter.predict(X[0])y_java=porter.predict([1.,2.,3.,4.])NotebooksYou can run and test all notebooks by starting a Jupyter notebook server locally:$makeopen.examples
$makestop.examplesCLIIn general you can use the porter on the command line:$ porter <pickle_file> [--to <directory>]
         [--class_name <class_name>] [--method_name <method_name>]
         [--export] [--checksum] [--data] [--pipe]
         [--c] [--java] [--js] [--go] [--php] [--ruby]
         [--version] [--help]The following example shows how you can save a trained estimator to thepickle format:# ...# Extract estimator:joblib.dump(clf,'estimator.pkl',compress=0)After that the estimator can be transpiled to JavaScript by using the following command:$porterestimator.pkl--jsThe target programming language is changeable on the fly:$porterestimator.pkl--c
$porterestimator.pkl--java
$porterestimator.pkl--php
$porterestimator.pkl--java
$porterestimator.pkl--rubyFor further processing the argument--pipecan be used to pass the result:$porterestimator.pkl--js--pipe>estimator.jsFor instance the result can be minified by usingUglifyJS:$porterestimator.pkl--js--pipe|uglifyjs--compress-oestimator.min.jsDevelopmentEnvironmentYou have to install required modules for broader development:$makeinstall.environment# conda environment (optional)$makeinstall.requirements.development# pip requirementsIndependently, the following compilers and intepreters are required to cover all tests:NameVersionCommandGCC>=4.2gcc --versionJava>=1.6java -versionPHP>=5.6php --versionRuby>=2.4.1ruby --versionGo>=1.7.4go versionNode.js>=6node --versionTestingThe tests cover module functions as well as matching predictions of transpiled estimators. Start all tests with:$maketestThe test files have a specific pattern:'[Algorithm][Language]Test.py':$pytesttests-v-opython_files='RandomForest*Test.py'$pytesttests-v-opython_files='*JavaTest.py'While you are developing new features or fixes, you can reduce the test duration by changing the number of tests:$N_RANDOM_FEATURE_SETS=5N_EXISTING_FEATURE_SETS=10\pytesttests-v-opython_files='*JavaTest.py'QualityIt's highly recommended to ensure the code quality. For thatPylintis used. Start the linter with:$makelintCitationIf you use this implementation in you work, please add a reference/citation to the paper. You can use the following BibTeX entry:@unpublished{skpodamo,
  author = {Darius Morawiec},
  title = {sklearn-porter},
  note = {Transpile trained scikit-learn estimators to C, Java, JavaScript and others},
  url = {https://github.com/nok/sklearn-porter}
}LicenseThe module is Open Source Software released under theMITlicense.Questions?Don't be shy and feel free to contact me onTwitterorGitter.