Skip to content

LightGBM models #580

Open
Open
@arthurwolf

Description

Hello.

(great project!)

I'm a bit lost of getting LightGBM (regression) models to work.

Are they supported? The main README seems to indicate they are, and the LightGBM project explicitely redirects here in the documentation when talking about generating code from the model.

I tried using the command line tool but I get this:

╰─⠠⠵ m2cgen -l c misc/models/test.txt                                                                                                                                                                                                                                                                                                   on main|✚1…6
Traceback (most recent call last):
  File "/home/arthur/.local/bin/m2cgen", line 8, in <module>
    sys.exit(main())
  File "/home/arthur/.local/lib/python3.10/site-packages/m2cgen/cli.py", line 137, in main
    print(generate_code(args))
  File "/home/arthur/.local/lib/python3.10/site-packages/m2cgen/cli.py", line 112, in generate_code
    model = pickle.load(f)
_pickle.UnpicklingError: could not find MARK

So I tried the python script I found in issue 99 : #99

import lightgbm as lgb
import m2cgen as m2c

model = lgb.Booster(model_file='misc/models/test.txt')

# This works but is awkward 
from lightgbm.sklearn import LGBMRegressor
r = LGBMRegressor()
r._Booster = model

code = m2c.export_to_java(r)

But that also fails:

╰─⠠⠵ python3 m2.py                                                                                                                                                                                                                                                                                                                      on main|✚1…6
Traceback (most recent call last):
  File "/home/arthur/dev/btc/champs/m2.py", line 11, in <module>
    code = m2c.export_to_java(r)
  File "/home/arthur/.local/lib/python3.10/site-packages/m2cgen/exporters.py", line 33, in export_to_java
    return _export(model, interpreter)
  File "/home/arthur/.local/lib/python3.10/site-packages/m2cgen/exporters.py", line 459, in _export
    model_ast = assembler_cls(model).assemble()
  File "/home/arthur/.local/lib/python3.10/site-packages/m2cgen/assemblers/boosting.py", line 222, in __init__
    model_dump = model.booster_.dump_model()
  File "/home/arthur/.local/lib/python3.10/site-packages/lightgbm/sklearn.py", line 854, in booster_
    raise LGBMNotFittedError('No booster found. Need to call fit beforehand.')
sklearn.exceptions.NotFittedError: No booster found. Need to call fit beforehand.

With another version of the script I did myself I get:

╰─⠠⠵ python3 m2.py                                                                                                                                                                                                                                                                                                                   on main|…6
Traceback (most recent call last):
  File "/home/arthur/dev/btc/champs/m2.py", line 8, in <module>
    code = m2c.export_to_java(bst);
  File "/home/arthur/.local/lib/python3.10/site-packages/m2cgen/exporters.py", line 33, in export_to_java
    return _export(model, interpreter)
  File "/home/arthur/.local/lib/python3.10/site-packages/m2cgen/exporters.py", line 458, in _export
    assembler_cls = get_assembler_cls(model)
  File "/home/arthur/.local/lib/python3.10/site-packages/m2cgen/assemblers/__init__.py", line 147, in get_assembler_cls
    raise NotImplementedError(f"Model '{model_name}' is not supported")
NotImplementedError: Model 'lightgbm_Booster' is not supported

Any idea what I'm doing wrong and how to get this to work?

Thanks a lot in advance!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions