Skip to content

Cannot export XGBClassifier model: TypeError: unsupported operand type(s) for /: 'float' and 'NoneType' #587

Open
@JudeWells

Description

import xgboost as xgb
import numpy as np
import m2cgen as m2c
from sklearn.datasets import load_boston
# train a model and save it as c code with m2cgen

X, y = load_boston(return_X_y=True)
y = np.random.choice([0, 1], size=len(y))
model = xgb.XGBClassifier(n_estimators=10, max_depth=3)
model.fit(X, y)
code = m2c.export_to_c(model)

Works fine with XGBoostRegressor

Full trace:

Traceback (most recent call last):
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/pydevd.py", line 1500, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/Users/judewells/Documents/dataScienceProgramming/tethir/ir3flamedetect2/experiment_scripts/temp_file_m2c.py", line 13, in <module>
    code = m2c.export_to_c(model)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/judewells/miniforge3/envs/bayes_opt/lib/python3.11/site-packages/m2cgen/exporters.py", line 81, in export_to_c
    return _export(model, interpreter)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/judewells/miniforge3/envs/bayes_opt/lib/python3.11/site-packages/m2cgen/exporters.py", line 459, in _export
    model_ast = assembler_cls(model).assemble()
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/judewells/miniforge3/envs/bayes_opt/lib/python3.11/site-packages/m2cgen/assemblers/boosting.py", line 214, in assemble
    return self.assembler.assemble()
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/judewells/miniforge3/envs/bayes_opt/lib/python3.11/site-packages/m2cgen/assemblers/boosting.py", line 34, in assemble
    return self._assemble_bin_class_output(self._all_estimator_params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/judewells/miniforge3/envs/bayes_opt/lib/python3.11/site-packages/m2cgen/assemblers/boosting.py", line 80, in _assemble_bin_class_output
    base_score = -math.log(1.0 / self._base_score - 1.0)
                           ~~~~^~~~~~~~~~~~~~~~~~
TypeError: unsupported operand type(s) for /: 'float' and 'NoneType'

Using m1 MacBook pro.
xgboost version '2.0.3' (also got the same error using older version)

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