Search code examples
pythonlightgbm

Missing method in lightgbm.Booster


It appears that the trees_to_dataframe method on a lightgbm.Booster objeect is missing. https://lightgbm.readthedocs.io/en/latest/pythonapi/lightgbm.Booster.html#lightgbm.Booster.trees_to_dataframe.

The following code raises an AttributeError on the last line.

import numpy as np
import pandas as pd
import lightgbm as lgb

number_of_classes = 5
x = np.random.random((100, 100))
y = np.random.randint(low=0, high=number_of_classes, size=100)
data = lgb.Dataset(x, y)

parameters = {
    'objective'             : 'multiclass',
    'num_class'             : number_of_classes,
    'metric'                : ['multi_logloss', ],
}

bst = lgb.train(parameters, data)

assert type(bst) == lgb.Booster
bst.trees_to_dataframe()

Solution

  • They updated recently their docs for the version 2.3.2 that has not been released to public yet.

    You have 2 options: either to wait for the next release or build from source.

    git clone --recursive https://github.com/Microsoft/LightGBM 
    cd LightGBM && mkdir build && cd build    
    cmake ..
    # cmake -DUSE_GPU=1 .. # build for GPU
    make -j12
    pip uninstall lightgbm
    cd ../python-package/
    python setup.py install