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, 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, "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.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: lgbm_to_code-0.2.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.tar.gz
Algorithm Hash digest
SHA256 dd50e255232800b655d0d6225338002011c9806a765890e96e89b870c83ab7a3
MD5 119a153a864c2171f8d3ed499bcb4cd5
BLAKE2b-256 8d7e1e6414ce34081cd73c9db6725902c29fc62ad2a644fea046f6ff2bb2dc5a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lgbm_to_code-0.2-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.19

File hashes

Hashes for lgbm_to_code-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 301ff9ed64f86e61db0877b244d01447c2c221e631e716ff1706a83d168e0931
MD5 79adfbff6f13dfaaa3b0447f89c40dd3
BLAKE2b-256 f308dd764e7a14fc8cf79ed794101d7dde6dd6fd6f983d6477aef5198c7c3042

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