Skip to main content

Convert a trained LGBM instance into conditionals that return the same output as a predict function. Supports javascript, python and C++.

Project description

lgbm-to-code

This package provides functionality to convert trained LightGBM models into native code for different programming languages. This allows you to deploy your models in environments where Python or LightGBM dependencies might not be readily available.

Installation

pip install lgbm-to-code

Usage

import lightgbm as lgb
from lgbm_to_code import lgbm_to_code

# Train your LightGBM model...
# For example:
from sklearn.datasets import load_diabetes
from sklearn.model_selection import train_test_split

diabetes = load_diabetes()
X = diabetes.data
y = diabetes.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = lgb.LGBMRegressor(random_state=42)
model.fit(X_train, y_train)

# Convert to desired language
languages = ["python", "cpp", "javascript"]
for language in languages:
    code = lgbm_to_code.parse_lgbm_model(model._Booster, language)
    with open(f"lgbm_model_{language}.{'py' if language == 'python' else language}", "w") as f:
        f.write(code)

Supported Languages

  • Python
  • C++
  • JavaScript

Example

import lightgbm as lgb
from sklearn.datasets import load_diabetes
from sklearn.model_selection import train_test_split
from lgbm_to_code import lgbm_to_code

# Load dataset and train model
diabetes = load_diabetes()
X = diabetes.data
y = diabetes.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = lgb.LGBMRegressor(random_state=42)
model.fit(X_train, y_train)

# Generate Python code
python_code = lgbm_to_code.parse_lgbm_model(model._Booster, "python")

# Save the code to a file
with open("lgbm_model.py", "w") as f:
    f.write(python_code)

# Now you can use this code in a separate Python environment

Limitations

  • Currently, the code generation only supports numerical features.
  • The generated code is not optimized for performance.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lgbm_to_code-0.2.1.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

lgbm_to_code-0.2.1-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file lgbm_to_code-0.2.1.tar.gz.

File metadata

  • Download URL: lgbm_to_code-0.2.1.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.19

File hashes

Hashes for lgbm_to_code-0.2.1.tar.gz
Algorithm Hash digest
SHA256 7fa24a417c6d88416052a877bcce8fdb95538109f73cbf4e803a656abc3cba37
MD5 48eeca92935f90df167c7f4e2246e085
BLAKE2b-256 3ac3ffb74f090459a2a1184904727a4c838f9fc6b3f3b2a3698bd2bdabcc2d95

See more details on using hashes here.

File details

Details for the file lgbm_to_code-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for lgbm_to_code-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 eb64308f783751675afa60dadfe3f37415ca6d56fa087c42be409e0dd6e6b0fc
MD5 c4366cec794b294bdbf48d5de6e8a5f6
BLAKE2b-256 f0ad483a20ec805a727c52b1322d12b5dd1ee848f87abd4c9eba9141f7a9c027

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page