Install m2cgen
masterInstall m2cgen using pip. Supported Python version is >= 3.7.
pip install m2cgenrepository·master·Indexed 25 days ago
https://github.com/bayeswitnesses/m2cgenA lightweight library that transpiles trained statistical models from libraries such as scikit-learn, XGBoost, and LightGBM into native code. It supports multiple target languages including C, C#, Java, Go, JavaScript, Python, Rust, and others via a Python API and a command-line interface.
Install m2cgen using pip. Supported Python version is >= 3.7.
pip install m2cgenThis occurs when generating code for ensemble models.
sys.setrecursionlimit(<new_depth>) in your Python environment.This happens when the pickle protocol cannot deserialize the model object.
If the generated code produces different results than the original Python model:
m2cgen works exclusively with float64 (double) data types. If your original model expects different types, try casting your input data to float64 manually.You can use the m2cgen Python library to export a trained model directly into native code. For example, to export a scikit-learn LinearRegression model to Java:
from sklearn.datasets import load_diabetes
from sklearn import linear_model
import m2cgen as m2c
X, y = load_diabetes(return_X_y=True)
estimator = linear_model.LinearRegression()
estimator.fit(X, y)
code = m2c.export_to_java(estimator)The output format of the generated code depends on the model type:
LinearClassifierMixin.decision_function.(n_samples, n_classes * (n_classes-1) / 2).BaseSVC.decision_function when decision_function_shape is set to ovo.predict_proba method of the original estimator (e.g., DecisionTreeClassifier, RandomForestClassifier, XGBClassifier, LGBMClassifier).The m2cgen CLI allows you to generate code from serialized model objects (pickle protocol).
Note: For unpickling to work, the classes of the serialized model must be defined in the top level of an importable module in the environment where you run the CLI.
$ 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>]Piping is also supported:
$ cat <pickle_file> | m2cgen --language <language>When using the --language or -l flag, you can choose from the following supported languages:
pythonjavacgojavascriptvisual_basicc_sharppowershellrphpdarthaskellrubyf_sharprustelixirThe following flags are available for the m2cgen CLI tool:
| Flag | Short | Description |
|---|---|---|
--language | -l | Required. The target language (e.g., python, java, c, go, javascript, c_sharp, rust, etc.). |
--function_name | -fn | Name of the generated function. |
--class_name | -cn | Name of the generated class (language dependent). |
--package_name | -pn | Package name for the generated code (language dependent). |
--module_name | -mn | Module name for the generated code (language dependent). |
--namespace | -ns | Namespace for the generated code (language dependent). |
--indent | -i | Indentation level for the generated code (default: 4). |
--recursion-limit | -rl | Sets the maximum depth of the Python interpreter stack (default: max int32). |
--pickle-lib | -pl | The library used to load the model file. Choices: pickle, joblib (default: pickle). |
--version | -v | Show version information. |
m2cgen command-line interface allows you to generate native code from a model file containing a pickle or joblib representation. You can provide the model file as a positional argument or pipe it via stdin.