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

Fixed missing feature names for XGBoost #93

Merged
merged 5 commits into from
Jul 30, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fixed missing feature names for XGBoost
  • Loading branch information
akhvorov committed Jul 17, 2019
commit 37775339a209e6399470537478ce9af30b72c001
11 changes: 7 additions & 4 deletions m2cgen/assemblers/boosting.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,11 @@ class XGBoostModelAssembler(BaseBoostingAssembler):

def __init__(self, model):
feature_names = model.get_booster().feature_names
krinart marked this conversation as resolved.
Show resolved Hide resolved
self._feature_name_to_idx = {
name: idx for idx, name in enumerate(feature_names)
}
self._feature_name_to_idx = {}
if feature_names is not None:
self._feature_name_to_idx = {
name: idx for idx, name in enumerate(feature_names)
}

model_dump = model.get_booster().get_dump(dump_format="json")
trees = [json.loads(d) for d in model_dump]
Expand All @@ -103,7 +105,8 @@ def _assemble_tree(self, tree):
return ast.NumVal(tree["leaf"])

threshold = ast.NumVal(tree["split_condition"])
feature_idx = self._feature_name_to_idx[tree["split"]]
split_feature = tree["split"]
feature_idx = self._feature_name_to_idx.get(split_feature, split_feature)
feature_ref = ast.FeatureRef(feature_idx)

# Since comparison with NaN (missing) value always returns false we
Expand Down