{"id":13413370,"url":"https://github.com/BayesWitnesses/m2cgen","last_synced_at":"2025-03-14T19:32:07.467Z","repository":{"id":38325607,"uuid":"165457814","full_name":"BayesWitnesses/m2cgen","owner":"BayesWitnesses","description":"Transform ML models into a native code (Java, C, Python, Go, JavaScript, Visual Basic, C#, R, PowerShell, PHP, Dart, Haskell, Ruby, F#, Rust) with zero dependencies","archived":false,"fork":false,"pushed_at":"2024-08-03T17:30:36.000Z","size":1284,"stargazers_count":2810,"open_issues_count":57,"forks_count":241,"subscribers_count":50,"default_branch":"master","last_synced_at":"2024-10-29T09:17:14.096Z","etag":null,"topics":["c","csharp","dartlang","go","haskell","java","javascript","lightgbm","lightning","machine-learning","php","python","r","ruby","rust","scikit-learn","statistical-learning","statsmodels","xgboost"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/BayesWitnesses.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-01-13T02:32:55.000Z","updated_at":"2024-10-24T21:54:33.000Z","dependencies_parsed_at":"2024-01-05T20:51:48.180Z","dependency_job_id":"d6eb4400-e95d-491e-993b-ef6bbd3ca0c5","html_url":"https://github.com/BayesWitnesses/m2cgen","commit_stats":{"total_commits":376,"total_committers":15,"mean_commits":"25.066666666666666","dds":0.6196808510638299,"last_synced_commit":"9784632311986234032673cdbfd29fc4c5cb429d"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BayesWitnesses%2Fm2cgen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BayesWitnesses%2Fm2cgen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BayesWitnesses%2Fm2cgen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BayesWitnesses%2Fm2cgen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/BayesWitnesses","download_url":"https://codeload.github.com/BayesWitnesses/m2cgen/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243119548,"owners_count":20239321,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","csharp","dartlang","go","haskell","java","javascript","lightgbm","lightning","machine-learning","php","python","r","ruby","rust","scikit-learn","statistical-learning","statsmodels","xgboost"],"created_at":"2024-07-30T20:01:38.787Z","updated_at":"2025-03-14T19:32:07.135Z","avatar_url":"https://github.com/BayesWitnesses.png","language":"Python","funding_links":[],"categories":["Machine Learning","AI","模板","Development tools","Artificial Intelligence","Python","开发工具 Development tools","Deep Learning Framework","工具","Data Science","Rust","人工智能","Deployment and Serving","Uncategorized","其他_机器学习与深度学习","Projects","Repositories","Tools","机器学习","模型序列化和转换","Machine Learning and Data Science","Misc","Machine Learning [🔝](#readme)","Templates","Machine Learning \u0026 AI on MCU","项目","Relational Databases","Model Transpilation"],"sub_categories":["机器学习","Transpiling","转化 Transpiling","Deployment \u0026 Distribution","Runner","Other dialects and variants","Uncategorized","Machine Learning","General-Purpose Machine Learning","检索及分析资料库","Search and Analytic Databases","USB","Advanced Console UIs"],"readme":"# m2cgen\n\n[![GitHub Actions Status](https://github.com/BayesWitnesses/m2cgen/workflows/GitHub%20Actions/badge.svg?branch=master)](https://github.com/BayesWitnesses/m2cgen/actions)\n[![Coverage Status](https://codecov.io/gh/BayesWitnesses/m2cgen/branch/master/graph/badge.svg)](https://codecov.io/gh/BayesWitnesses/m2cgen)\n[![License: MIT](https://img.shields.io/github/license/BayesWitnesses/m2cgen.svg)](https://github.com/BayesWitnesses/m2cgen/blob/master/LICENSE)\n[![Python Versions](https://img.shields.io/pypi/pyversions/m2cgen.svg?logo=python\u0026logoColor=white)](https://pypi.org/project/m2cgen)\n[![PyPI Version](https://img.shields.io/pypi/v/m2cgen.svg?logo=pypi\u0026logoColor=white)](https://pypi.org/project/m2cgen)\n[![Downloads](https://pepy.tech/badge/m2cgen)](https://pepy.tech/project/m2cgen)\n\n**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, Haskell, Ruby, F#, Rust, Elixir).\n\n* [Installation](#installation)\n* [Development](#development)\n* [Supported Languages](#supported-languages)\n* [Supported Models](#supported-models)\n* [Classification Output](#classification-output)\n* [Usage](#usage)\n* [CLI](#cli)\n* [FAQ](#faq)\n\n## Installation\nSupported Python version is \u003e= **3.7**.\n```\npip install m2cgen\n```\n\n## Development\nMake sure the following command runs successfully before submitting a PR:\n```\nmake pre-pr\n```\nAlternatively you can run the Docker version of the same command:\n```\nmake docker-build docker-pre-pr\n```\n\n## Supported Languages\n\n- C\n- C#\n- Dart\n- F#\n- Go\n- Haskell\n- Java\n- JavaScript\n- PHP\n- PowerShell\n- Python\n- R\n- Ruby\n- Rust\n- Visual Basic (VBA-compatible)\n- Elixir\n\n## Supported Models\n\n|  | Classification | Regression |\n| --- | --- | --- |\n| **Linear** | \u003cul\u003e\u003cli\u003escikit-learn\u003cul\u003e\u003cli\u003eLogisticRegression\u003c/li\u003e\u003cli\u003eLogisticRegressionCV\u003c/li\u003e\u003cli\u003ePassiveAggressiveClassifier\u003c/li\u003e\u003cli\u003ePerceptron\u003c/li\u003e\u003cli\u003eRidgeClassifier\u003c/li\u003e\u003cli\u003eRidgeClassifierCV\u003c/li\u003e\u003cli\u003eSGDClassifier\u003c/li\u003e\u003c/ul\u003e\u003c/li\u003e\u003cli\u003elightning\u003cul\u003e\u003cli\u003eAdaGradClassifier\u003c/li\u003e\u003cli\u003eCDClassifier\u003c/li\u003e\u003cli\u003eFistaClassifier\u003c/li\u003e\u003cli\u003eSAGAClassifier\u003c/li\u003e\u003cli\u003eSAGClassifier\u003c/li\u003e\u003cli\u003eSDCAClassifier\u003c/li\u003e\u003cli\u003eSGDClassifier\u003c/li\u003e\u003c/ul\u003e\u003c/li\u003e\u003c/ul\u003e | \u003cul\u003e\u003cli\u003escikit-learn\u003cul\u003e\u003cli\u003eARDRegression\u003c/li\u003e\u003cli\u003eBayesianRidge\u003c/li\u003e\u003cli\u003eElasticNet\u003c/li\u003e\u003cli\u003eElasticNetCV\u003c/li\u003e\u003cli\u003eGammaRegressor\u003c/li\u003e\u003cli\u003eHuberRegressor\u003c/li\u003e\u003cli\u003eLars\u003c/li\u003e\u003cli\u003eLarsCV\u003c/li\u003e\u003cli\u003eLasso\u003c/li\u003e\u003cli\u003eLassoCV\u003c/li\u003e\u003cli\u003eLassoLars\u003c/li\u003e\u003cli\u003eLassoLarsCV\u003c/li\u003e\u003cli\u003eLassoLarsIC\u003c/li\u003e\u003cli\u003eLinearRegression\u003c/li\u003e\u003cli\u003eOrthogonalMatchingPursuit\u003c/li\u003e\u003cli\u003eOrthogonalMatchingPursuitCV\u003c/li\u003e\u003cli\u003ePassiveAggressiveRegressor\u003c/li\u003e\u003cli\u003ePoissonRegressor\u003c/li\u003e\u003cli\u003eRANSACRegressor(only supported regression estimators can be used as a base estimator)\u003c/li\u003e\u003cli\u003eRidge\u003c/li\u003e\u003cli\u003eRidgeCV\u003c/li\u003e\u003cli\u003eSGDRegressor\u003c/li\u003e\u003cli\u003eTheilSenRegressor\u003c/li\u003e\u003cli\u003eTweedieRegressor\u003c/li\u003e\u003c/ul\u003e\u003cli\u003eStatsModels\u003cul\u003e\u003cli\u003eGeneralized Least Squares (GLS)\u003c/li\u003e\u003cli\u003eGeneralized Least Squares with AR Errors (GLSAR)\u003c/li\u003e\u003cli\u003eGeneralized Linear Models (GLM)\u003c/li\u003e\u003cli\u003eOrdinary Least Squares (OLS)\u003c/li\u003e\u003cli\u003e[Gaussian] Process Regression Using Maximum Likelihood-based Estimation (ProcessMLE)\u003c/li\u003e\u003cli\u003eQuantile Regression (QuantReg)\u003c/li\u003e\u003cli\u003eWeighted Least Squares (WLS)\u003c/li\u003e\u003c/ul\u003e\u003cli\u003elightning\u003cul\u003e\u003cli\u003eAdaGradRegressor\u003c/li\u003e\u003cli\u003eCDRegressor\u003c/li\u003e\u003cli\u003eFistaRegressor\u003c/li\u003e\u003cli\u003eSAGARegressor\u003c/li\u003e\u003cli\u003eSAGRegressor\u003c/li\u003e\u003cli\u003eSDCARegressor\u003c/li\u003e\u003cli\u003eSGDRegressor\u003c/li\u003e\u003c/ul\u003e\u003c/li\u003e\u003c/ul\u003e |\n| **SVM** | \u003cul\u003e\u003cli\u003escikit-learn\u003cul\u003e\u003cli\u003eLinearSVC\u003c/li\u003e\u003cli\u003eNuSVC\u003c/li\u003e\u003cli\u003eOneClassSVM\u003c/li\u003e\u003cli\u003eSVC\u003c/li\u003e\u003c/ul\u003e\u003c/li\u003e\u003cli\u003elightning\u003cul\u003e\u003cli\u003eKernelSVC\u003c/li\u003e\u003cli\u003eLinearSVC\u003c/li\u003e\u003c/ul\u003e\u003c/li\u003e\u003c/ul\u003e | \u003cul\u003e\u003cli\u003escikit-learn\u003cul\u003e\u003cli\u003eLinearSVR\u003c/li\u003e\u003cli\u003eNuSVR\u003c/li\u003e\u003cli\u003eSVR\u003c/li\u003e\u003c/ul\u003e\u003c/li\u003e\u003cli\u003elightning\u003cul\u003e\u003cli\u003eLinearSVR\u003c/li\u003e\u003c/ul\u003e\u003c/li\u003e\u003c/ul\u003e |\n| **Tree** | \u003cul\u003e\u003cli\u003eDecisionTreeClassifier\u003c/li\u003e\u003cli\u003eExtraTreeClassifier\u003c/li\u003e\u003c/ul\u003e | \u003cul\u003e\u003cli\u003eDecisionTreeRegressor\u003c/li\u003e\u003cli\u003eExtraTreeRegressor\u003c/li\u003e\u003c/ul\u003e |\n| **Random Forest** | \u003cul\u003e\u003cli\u003eExtraTreesClassifier\u003c/li\u003e\u003cli\u003eLGBMClassifier(rf booster only)\u003c/li\u003e\u003cli\u003eRandomForestClassifier\u003c/li\u003e\u003cli\u003eXGBRFClassifier\u003c/li\u003e\u003c/ul\u003e | \u003cul\u003e\u003cli\u003eExtraTreesRegressor\u003c/li\u003e\u003cli\u003eLGBMRegressor(rf booster only)\u003c/li\u003e\u003cli\u003eRandomForestRegressor\u003c/li\u003e\u003cli\u003eXGBRFRegressor\u003c/li\u003e\u003c/ul\u003e |\n| **Boosting** | \u003cul\u003e\u003cli\u003eLGBMClassifier(gbdt/dart/goss booster only)\u003c/li\u003e\u003cli\u003eXGBClassifier(gbtree(including boosted forests)/gblinear booster only)\u003c/li\u003e\u003cul\u003e | \u003cul\u003e\u003cli\u003eLGBMRegressor(gbdt/dart/goss booster only)\u003c/li\u003e\u003cli\u003eXGBRegressor(gbtree(including boosted forests)/gblinear booster only)\u003c/li\u003e\u003c/ul\u003e |\n\nYou can find versions of packages with which compatibility is guaranteed by CI tests [here](https://github.com/BayesWitnesses/m2cgen/blob/master/requirements-test.txt#L1).\nOther versions can also be supported but they are untested.\n\n## Classification Output\n### Linear / Linear SVM / Kernel SVM\n#### Binary\nScalar value; signed distance of the sample to the hyperplane for the second class.\n#### Multiclass\nVector value; signed distance of the sample to the hyperplane per each class.\n#### Comment\nThe output is consistent with the output of ```LinearClassifierMixin.decision_function```.\n\n### SVM\n#### Outlier detection\nScalar value; signed distance of the sample to the separating hyperplane: positive for an inlier and negative for an outlier.\n#### Binary\nScalar value; signed distance of the sample to the hyperplane for the second class.\n#### Multiclass\nVector value; one-vs-one score for each class, shape (n_samples, n_classes * (n_classes-1) / 2).\n#### Comment\nThe output is consistent with the output of ```BaseSVC.decision_function``` when the `decision_function_shape` is set to `ovo`.\n\n### Tree / Random Forest / Boosting\n#### Binary\nVector value; class probabilities.\n#### Multiclass\nVector value; class probabilities.\n#### Comment\nThe output is consistent with the output of the `predict_proba` method of `DecisionTreeClassifier` / `ExtraTreeClassifier` / `ExtraTreesClassifier` / `RandomForestClassifier` / `XGBRFClassifier` / `XGBClassifier` / `LGBMClassifier`.\n\n## Usage\n\nHere's a simple example of how a linear model trained in Python environment can be represented in Java code:\n```python\nfrom sklearn.datasets import load_diabetes\nfrom sklearn import linear_model\nimport m2cgen as m2c\n\nX, y = load_diabetes(return_X_y=True)\n\nestimator = linear_model.LinearRegression()\nestimator.fit(X, y)\n\ncode = m2c.export_to_java(estimator)\n```\n\nGenerated Java code:\n```java\npublic class Model {\n    public static double score(double[] input) {\n        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));\n    }\n}\n```\n\n**You can find more examples of generated code for different models/languages [here](https://github.com/BayesWitnesses/m2cgen/tree/master/generated_code_examples).**\n\n## CLI\n\n`m2cgen` can be used as a CLI tool to generate code using serialized model objects (pickle protocol):\n```\n$ m2cgen \u003cpickle_file\u003e --language \u003clanguage\u003e [--indent \u003cindent\u003e] [--function_name \u003cfunction_name\u003e]\n         [--class_name \u003cclass_name\u003e] [--module_name \u003cmodule_name\u003e] [--package_name \u003cpackage_name\u003e]\n         [--namespace \u003cnamespace\u003e] [--recursion-limit \u003crecursion_limit\u003e]\n```\nDon'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.\n\nPiping is also supported:\n```\n$ cat \u003cpickle_file\u003e | m2cgen --language \u003clanguage\u003e\n```\n\n## FAQ\n**Q: Generation fails with `RecursionError: maximum recursion depth exceeded` error.**\n\nA: 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(\u003cnew_depth\u003e)`.\n\n**Q: Generation fails with `ImportError: No module named \u003cmodule_name_here\u003e` error while transpiling model from a serialized model object.**\n\nA: 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.\n\n**Q: Generated by m2cgen code provides different results for some inputs compared to original Python model from which the code were obtained.**\n\nA: Some models force input data to be particular type during prediction phase in their native Python libraries. Currently, m2cgen works only with ``float64`` (``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.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FBayesWitnesses%2Fm2cgen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FBayesWitnesses%2Fm2cgen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FBayesWitnesses%2Fm2cgen/lists"}