Search code examples
pythonc++machine-learninglightgbm

Save LGBM model in `.cpp` format from Python


If I run

from sklearn.datasets import load_breast_cancer
import lightgbm as lgb

breast_cancer = load_breast_cancer()
data = breast_cancer.data
target = breast_cancer.target

params = {
    "task": "convert_model",
    "convert_model_language": "cpp",
    "convert_model": "test.cpp",
}

gbm = lgb.train(params, lgb.Dataset(data, target))

then I was expecting that a file called test.cpp would be created, with the model saved in c++ format.

However, nothing appears in my current directory.

I have read the documentation (https://lightgbm.readthedocs.io/en/latest/Parameters.html#io-parameters), but can't tell what I'm doing wrong.


Solution

  • In the doc they say:

    Note: can be used only in CLI version

    under the convert_model and convert_model_language parameters.

    That means that you should probably use the CLI (Command Line Interfarce) of LGBM instead of the python wrapper to do this.

    Link to Quick Start CLI version.