Search code examples
onnxxgbclassifier

How to solve Feature name error while converting an XGBClassifier model to ONNX?


I trained an XGBClassifier model, and now I want to convert it to an ONNX format. it should be straight forward using this code:

import onnxmltools 
from skl2onnx.common.data_types import FloatTensorType

initial_types = [('float_input', FloatTensorType([None, X_train.shape[1]]))]

xgb_onnx = onnxmltools.convert_xgboost(xgb.xgb_category_cls, initial_types=initial_types)
onnxmltools.utils.save_model(xgb_onnx , 'xgb_onnx .onnx')

However, I get this error which is related to one of my features name:

     77                     feature_id = int(float(feature_id))
     78                 except ValueError:
---> 79                     raise RuntimeError(
     80                         "Unable to interpret '{0}', feature "
     81                         "names should follow pattern 'f%d'.".format(

RuntimeError: Unable to interpret 'state', feature names should follow pattern 'f%d'.

I am not sure what I did wrong.


Solution

  • I was able to mitigate the error by renaming the feature names to the following form:

    f0, f1, f2, ....., fn
    

    That was my workaround.