Skip to content

Commit

Permalink
add support of non-string categorical features
Browse files Browse the repository at this point in the history
Internaly, ebm converts categorical feature to a unicode string, whatever their
actual type. So we keep the existing categorical encoding and simply cast the
feature to strings before doing the lookup.

Fixes #1
  • Loading branch information
MainRo committed Aug 2, 2022
1 parent d2acfe7 commit 2111967
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
7 changes: 2 additions & 5 deletions ebm2onnx/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,9 @@ def to_onnx(model, dtype, name="ebm",
additive_terms = model.additive_terms_[feature_index]

feature_dtype = infer_features_dtype(dtype, feature_name)
if feature_dtype != onnx.TensorProto.STRING:
raise ValueError(
"categorical features must be encoded as strings only. "
"{} is encoded as {} which is not supported.".format(feature_name, dtype[feature_name])
)
part = graph.create_input(root, feature_name, feature_dtype, [None])
if feature_dtype != onnx.TensorProto.STRING:
part = ops.cast(onnx.TensorProto.STRING)(part)
part = ops.flatten()(part)
inputs[feature_index] = part
part = ebm.get_bin_index_on_categorical_value(col_mapping)(part)
Expand Down
11 changes: 6 additions & 5 deletions tests/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ def train_titanic_binary_classification(interactions, with_categorical=False):
)
df = df.dropna()
df['Old'] = df['Age'] > 65
feature_types=['continuous', 'continuous', 'continuous', 'continuous']
feature_columns = ['Age', 'Fare', 'Pclass', 'Old']
if with_categorical is True:
feature_columns.append('Embarked')
feature_types.append('categorical')
if with_categorical is False:
feature_types=['continuous', 'continuous', 'continuous', 'continuous']
feature_columns = ['Age', 'Fare', 'Pclass', 'Old']
else:
feature_types=['continuous', 'continuous', 'categorical', 'continuous', 'categorical']
feature_columns = ['Age', 'Fare', 'Pclass', 'Old', 'Embarked']
label_column = "Survived"

y = df[[label_column]]
Expand Down

0 comments on commit 2111967

Please sign in to comment.