Package: m2cgen
m2cgenm2cgen(Model 2 Code Generator) - is a lightweight library which provides an easy way to transpile trained statistical models into a native code (Python, C, Java, Go, JavaScript, Visual Basic, C#, PowerShell, R, PHP, Dart, Haskell, Ruby, F#, Rust, Elixir).InstallationSupported LanguagesSupported ModelsClassification OutputUsageCLIFAQInstallationSupported Python version is >=3.7.pip install m2cgenSupported LanguagesCC#DartF#GoHaskellJavaJavaScriptPHPPowerShellPythonRRubyRustVisual Basic (VBA-compatible)ElixirSupported ModelsClassificationRegressionLinearscikit-learnLogisticRegressionLogisticRegressionCVPassiveAggressiveClassifierPerceptronRidgeClassifierRidgeClassifierCVSGDClassifierlightningAdaGradClassifierCDClassifierFistaClassifierSAGAClassifierSAGClassifierSDCAClassifierSGDClassifierscikit-learnARDRegressionBayesianRidgeElasticNetElasticNetCVGammaRegressorHuberRegressorLarsLarsCVLassoLassoCVLassoLarsLassoLarsCVLassoLarsICLinearRegressionOrthogonalMatchingPursuitOrthogonalMatchingPursuitCVPassiveAggressiveRegressorPoissonRegressorRANSACRegressor(only supported regression estimators can be used as a base estimator)RidgeRidgeCVSGDRegressorTheilSenRegressorTweedieRegressorStatsModelsGeneralized Least Squares (GLS)Generalized Least Squares with AR Errors (GLSAR)Generalized Linear Models (GLM)Ordinary Least Squares (OLS)[Gaussian] Process Regression Using Maximum Likelihood-based Estimation (ProcessMLE)Quantile Regression (QuantReg)Weighted Least Squares (WLS)lightningAdaGradRegressorCDRegressorFistaRegressorSAGARegressorSAGRegressorSDCARegressorSGDRegressorSVMscikit-learnLinearSVCNuSVCOneClassSVMSVClightningKernelSVCLinearSVCscikit-learnLinearSVRNuSVRSVRlightningLinearSVRTreeDecisionTreeClassifierExtraTreeClassifierDecisionTreeRegressorExtraTreeRegressorRandom ForestExtraTreesClassifierLGBMClassifier(rf booster only)RandomForestClassifierXGBRFClassifierExtraTreesRegressorLGBMRegressor(rf booster only)RandomForestRegressorXGBRFRegressorBoostingLGBMClassifier(gbdt/dart/goss booster only)XGBClassifier(gbtree(including boosted forests)/gblinear booster only)LGBMRegressor(gbdt/dart/goss booster only)XGBRegressor(gbtree(including boosted forests)/gblinear booster only)You can find versions of packages with which compatibility is guaranteed by CI testshere.
Other versions can also be supported but they are untested.Classification OutputLinear / Linear SVM / Kernel SVMBinaryScalar value; signed distance of the sample to the hyperplane for the second class.MulticlassVector value; signed distance of the sample to the hyperplane per each class.CommentThe output is consistent with the output ofLinearClassifierMixin.decision_function.SVMOutlier detectionScalar value; signed distance of the sample to the separating hyperplane: positive for an inlier and negative for an outlier.BinaryScalar value; signed distance of the sample to the hyperplane for the second class.MulticlassVector value; one-vs-one score for each class, shape (n_samples, n_classes * (n_classes-1) / 2).CommentThe output is consistent with the output ofBaseSVC.decision_functionwhen thedecision_function_shapeis set toovo.Tree / Random Forest / BoostingBinaryVector value; class probabilities.MulticlassVector value; class probabilities.CommentThe output is consistent with the output of thepredict_probamethod ofDecisionTreeClassifier/ExtraTreeClassifier/ExtraTreesClassifier/RandomForestClassifier/XGBRFClassifier/XGBClassifier/LGBMClassifier.UsageHere's a simple example of how a linear model trained in Python environment can be represented in Java code:fromsklearn.datasetsimportload_diabetesfromsklearnimportlinear_modelimportm2cgenasm2cX,y=load_diabetes(return_X_y=True)estimator=linear_model.LinearRegression()estimator.fit(X,y)code=m2c.export_to_java(estimator)Generated Java code:publicclassModel{publicstaticdoublescore(double[]input){return((((((((((152.1334841628965)+((input[0])*(-10.012197817470472)))+((input[1])*(-239.81908936565458)))+((input[2])*(519.8397867901342)))+((input[3])*(324.39042768937657)))+((input[4])*(-792.1841616283054)))+((input[5])*(476.74583782366153)))+((input[6])*(101.04457032134408)))+((input[7])*(177.06417623225025)))+((input[8])*(751.2793210873945)))+((input[9])*(67.62538639104406));}}You can find more examples of generated code for different models/languageshere.CLIm2cgencan be used as a CLI tool to generate code using serialized model objects (pickle protocol):$ m2cgen <pickle_file> --language <language> [--indent <indent>] [--function_name <function_name>]
         [--class_name <class_name>] [--module_name <module_name>] [--package_name <package_name>]
         [--namespace <namespace>] [--recursion-limit <recursion_limit>]Don't forget that for unpickling serialized model objects their classes must be defined in the top level of an importable module in the unpickling environment.Piping is also supported:$ cat <pickle_file> | m2cgen --language <language>FAQQ: Generation fails withRecursionError: maximum recursion depth exceedederror.A: If this error occurs while generating code using an ensemble model, try to reduce the number of trained estimators within that model. Alternatively you can increase the maximum recursion depth withsys.setrecursionlimit(<new_depth>).Q: Generation fails withImportError: No module named <module_name_here>error while transpiling model from a serialized model object.A: This error indicates that pickle protocol cannot deserialize model object. For unpickling serialized model objects, it is required that their classes must be defined in the top level of an importable module in the unpickling environment. So installation of package which provided model's class definition should solve the problem.Q: Generated by m2cgen code provides different results for some inputs compared to original Python model from which the code were obtained.A: Some models force input data to be particular type during prediction phase in their native Python libraries. Currently, m2cgen works only withfloat64(double) data type. You can try to cast your input data to another type manually and check results again. Also, some small differences can happen due to specific implementation of floating-point arithmetic in a target language.