A model that can't be reloaded exactly in production is not finished.
LightGBM models have a native text format:
booster.save_model("model.txt")(ormodel.booster_.save_model(...)from the scikit-learn wrapper) writes all trees and parameters. Passnum_iteration=booster.best_iterationto save only the best trees.lgb.Booster(model_file="model.txt")loads it;booster.predict(X)works immediately.booster.model_to_string()/lgb.Booster(model_str=...)do the same in memory, handy for storing models in databases.booster.dump_model()returns a JSON-like dict of every tree, useful for inspection or custom runtimes.
The text format is readable by the C API, CLI and other language bindings, which is why many production systems serve LightGBM from Java, C++ or via ONNX converters.
The scikit-learn wrapper can also be pickled with joblib, which keeps attributes like classes_ and works inside full pipelines, but ties the file to library versions; only load pickles you trust.
Serving pitfalls:
- Column order and dtypes must match training. With a DataFrame, keep the same columns in the same order and the same categorical categories.
- Predictions from a loaded
Boosterare probabilities for binary objectives, not labels. - Record the LightGBM version and parameters alongside the model.
Example
import lightgbm as lgb
import numpy as np
from sklearn.datasets import load_breast_cancer
X, y = load_breast_cancer(return_X_y=True)
model = lgb.LGBMClassifier(n_estimators=100, verbose=-1).fit(X, y)
model.booster_.save_model("cancer_lgb.txt")
booster = lgb.Booster(model_file="cancer_lgb.txt")
proba = booster.predict(X[:3]) # probabilities, not labels
print(proba.round(3)) # [0. 0. 0.]
print(np.allclose(proba, model.predict_proba(X[:3])[:, 1])) # True
text = booster.model_to_string()
print(text.splitlines()[0], booster.num_trees()) # tree 100Remember
save_modelwrites a portable text format;lgb.Booster(model_file=...)loads it- A loaded Booster's
predictreturns probabilities for binary objectives - Keep column order, dtypes and categories identical at serving time
Watch out for
- Feeding columns in a different order at serving time, which silently produces wrong predictions.