Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
JudeWells opened this issue Feb 8, 2024 · 4 comments

Comments

@JudeWells
Copy link

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)

@rshanker779
Copy link

I'm also having the same issue

@yhfwww
Copy link

yhfwww commented Mar 14, 2024

我也有同样的问题

@JudeWells
Copy link
Author

You can fix this by setting the base_score parameter in your xgboost model before passing to m2cgen:
model = xgb.XGBClassifier(n_estimators=10, max_depth=3, base_score=0.5)

@kx-kexi
Copy link

kx-kexi commented Apr 1, 2024

You can fix this by setting the base_score parameter in your xgboost model before passing to m2cgen: model = xgb.XGBClassifier(n_estimators=10, max_depth=3, base_score=0.5)

how can i choose the value of base_score

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

No branches or pull requests

4 participants