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

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: lgbm_to_code-0.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.1.tar.gz
Algorithm Hash digest
SHA256 66e3afa63440ec41234fb4b6e012424314780189a2f6b9f7429b1351d96476ac
MD5 081759b6dc758c80e3861efce9bc356d
BLAKE2b-256 750ef19ae4d3e8784eb8024ea4eacc55dcbd0a14b391a886e6b50063f776c9fb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lgbm_to_code-0.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 85145a5dfad8f30369341b7da8581e8631e7ca132bdbad8c03fb057ada961747
MD5 560ac1158209b8b44e97bb270263a2f2
BLAKE2b-256 5a1e043d34837b506dea537ce3d845edeecb3befb4f0fa16203afe7e4211fe12

See more details on using hashes here.

Supported by

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