BayesWitnesses / m2cgen

Transform ML models into a native code (Java, C, Python, Go, JavaScript, Visual Basic, C#, R, PowerShell, PHP, Dart, Haskell, Ruby, F#, Rust) with zero dependencies
MIT License
2.82k stars 241 forks source link

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

Open JudeWells opened 9 months ago

JudeWells commented 9 months ago
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 commented 9 months ago

I'm also having the same issue

yhfwww commented 8 months ago

我也有同样的问题

JudeWells commented 8 months ago

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 commented 8 months ago

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