Git Product home page Git Product logo

mbyase / m2cgen Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bayeswitnesses/m2cgen

0.0 1.0 0.0 362 KB

Transform ML models into a native code (Java, C, Python, Go, JavaScript, Visual Basic, C#, R, PowerShell, PHP) with zero dependencies

License: MIT License

Python 44.64% Java 8.61% C 5.65% Dockerfile 0.15% Makefile 0.07% Go 6.13% JavaScript 1.18% C# 6.12% Visual Basic .NET 6.64% VBA 0.13% PowerShell 7.36% Shell 0.20% R 7.02% PHP 6.03% Dart 0.07%

m2cgen's Introduction

m2cgen

Build Status Coverage Status License: MIT Python Versions PyPI Version Downloads

m2cgen (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).

Installation

Supported Python version is >= 3.5.

pip install m2cgen

Supported Languages

  • C
  • C#
  • Dart
  • Go
  • Java
  • JavaScript
  • PHP
  • PowerShell
  • Python
  • R
  • Visual Basic

Supported Models

Classification Regression
Linear
  • LogisticRegression
  • LogisticRegressionCV
  • PassiveAggressiveClassifier
  • Perceptron
  • RidgeClassifier
  • RidgeClassifierCV
  • SGDClassifier
  • scikit-learn
    • ARDRegression
    • BayesianRidge
    • ElasticNet
    • ElasticNetCV
    • HuberRegressor
    • Lars
    • LarsCV
    • Lasso
    • LassoCV
    • LassoLars
    • LassoLarsCV
    • LassoLarsIC
    • LinearRegression
    • OrthogonalMatchingPursuit
    • OrthogonalMatchingPursuitCV
    • PassiveAggressiveRegressor
    • RANSACRegressor(only supported regression estimators can be used as a base estimator)
    • Ridge
    • RidgeCV
    • SGDRegressor
    • TheilSenRegressor
  • StatsModels
    • Generalized Least Squares (GLS)
    • Generalized Least Squares with AR Errors (GLSAR)
    • Ordinary Least Squares (OLS)
    • Weighted Least Squares (WLS)
    SVM
    • LinearSVC
    • NuSVC
    • SVC
    • LinearSVR
    • NuSVR
    • SVR
    Tree
    • DecisionTreeClassifier
    • ExtraTreeClassifier
    • DecisionTreeRegressor
    • ExtraTreeRegressor
    Random Forest
    • ExtraTreesClassifier
    • LGBMClassifier(rf booster only)
    • RandomForestClassifier
    • XGBRFClassifier(binary only, multiclass is not supported yet)
    • ExtraTreesRegressor
    • LGBMRegressor(rf booster only)
    • RandomForestRegressor
    • XGBRFRegressor
    Boosting
    • LGBMClassifier(gbdt/dart/goss booster only)
    • XGBClassifier(gbtree/gblinear booster only)
      • LGBMRegressor(gbdt/dart/goss booster only)
      • XGBRegressor(gbtree/gblinear booster only)

      Classification Output

      Linear/Linear SVM

      Binary

      Scalar value; signed distance of the sample to the hyperplane for the second class.

      Multiclass

      Vector value; signed distance of the sample to the hyperplane per each class.

      Comment

      The output is consistent with the output of LinearClassifierMixin.decision_function.

      SVM

      Binary

      Scalar value; signed distance of the sample to the hyperplane for the second class.

      Multiclass

      Vector value; one-vs-one score for each class, shape (n_samples, n_classes * (n_classes-1) / 2).

      Comment

      The output is consistent with the output of BaseSVC.decision_function when the decision_function_shape is set to ovo.

      Tree/Random Forest/XGBoost/LightGBM

      Binary

      Vector value; class probabilities.

      Multiclass

      Vector value; class probabilities.

      Comment

      The output is consistent with the output of the predict_proba method of DecisionTreeClassifier/ForestClassifier/XGBClassifier/LGBMClassifier.

      Usage

      Here's a simple example of how a linear model trained in Python environment can be represented in Java code:

      from sklearn.datasets import load_boston
      from sklearn import linear_model
      import m2cgen as m2c
      
      boston = load_boston()
      X, y = boston.data, boston.target
      
      estimator = linear_model.LinearRegression()
      estimator.fit(X, y)
      
      code = m2c.export_to_java(estimator)

      Generated Java code:

      public class Model {
      
          public static double score(double[] input) {
              return (((((((((((((36.45948838508965) + ((input[0]) * (-0.10801135783679647))) + ((input[1]) * (0.04642045836688297))) + ((input[2]) * (0.020558626367073608))) + ((input[3]) * (2.6867338193449406))) + ((input[4]) * (-17.76661122830004))) + ((input[5]) * (3.8098652068092163))) + ((input[6]) * (0.0006922246403454562))) + ((input[7]) * (-1.475566845600257))) + ((input[8]) * (0.30604947898516943))) + ((input[9]) * (-0.012334593916574394))) + ((input[10]) * (-0.9527472317072884))) + ((input[11]) * (0.009311683273794044))) + ((input[12]) * (-0.5247583778554867));
          }
      }

      You can find more examples of generated code for different models/languages here.

      CLI

      m2cgen can 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>
      

      FAQ

      Q: Generation fails with RuntimeError: maximum recursion depth exceeded error.

      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 with sys.setrecursionlimit(<new_depth>).

      Q: Generation fails with ImportError: 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.

      m2cgen's People

      Contributors

      akhvorov avatar arshamg avatar bcampbell-prosper avatar izeigerman avatar krinart avatar matbur avatar mattconflitti avatar mrshu avatar strikerrus avatar

      Watchers

       avatar

      Recommend Projects

      • React photo React

        A declarative, efficient, and flexible JavaScript library for building user interfaces.

      • Vue.js photo Vue.js

        ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

      • Typescript photo Typescript

        TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

      • TensorFlow photo TensorFlow

        An Open Source Machine Learning Framework for Everyone

      • Django photo Django

        The Web framework for perfectionists with deadlines.

      • D3 photo D3

        Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

      Recommend Topics

      • javascript

        JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

      • web

        Some thing interesting about web. New door for the world.

      • server

        A server is a program made to process requests and deliver data to clients.

      • Machine learning

        Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

      • Game

        Some thing interesting about game, make everyone happy.

      Recommend Org

      • Facebook photo Facebook

        We are working to build community through open source technology. NB: members must have two-factor auth.

      • Microsoft photo Microsoft

        Open source projects and samples from Microsoft.

      • Google photo Google

        Google โค๏ธ Open Source for everyone.

      • D3 photo D3

        Data-Driven Documents codes.