Add support to convert xgboost models with categorical features #734
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently, when creating a xgboost model with at least one of the features being a categorical feature (using enable_categorical=True) the conversion to onnx fails.
In this pr, I am proposing to add support for converting categorical features in a xgboost model similarly as is done for lightgbm where the conversion works already.
Context: Usually, the decision trees of a GBM model use numeric comparisons (e.g. go left if feature < x). However, categorical features split based on set operations (e.g. go left if feature has one of categories [A, B, D]).
For xgboost these set comparisons are implemented as a list of categories in the "split_condition" field instead of a number for the numeric comparison.
The TreeEnsemble operator in onnx does not directly (currently) support set operations, but it does support equality checks.
The set operation can be represented in the TreeEnsemble operator as multiple equality checks, using additional branches in the decision tree.
Example 1: the split condition go left (id 1) if feature is one of categories [A] otherwise go right (id 2) can easily represented as:
id 0: go to id 1 if feature == A, else go to id 2
Example 2: the split condition go left (id 1) if feature is one of categories [A, B] otherwise go right (id 2) can be represented as:
id 0: go to id 1 if feature == A, else go to id 3
id 3: go to copy of id 1 if feature == B, else go to id 2
Some edge cases need to be accounted for: left and right can be flipped; the behavior when input is missing also needs to be handled.
In this pr proposing an implementation of this conversion for xgboost models. I added unit tests to check that this conversion works correctly for a number of different xgboost setups.
Notes:
This is a similar feature to what lightgbm can do when using pandas. Neither of these can be used for the onnxmlconverter in its current form as it currently only supports numeric float inputs. So this change has no effect here. The onnxmlconverter will only use the final category codes. To run inference with the onnx file the category codes need to be used as inputs.