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
Show file tree
Hide file tree
Changes from 4 commits
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
5 changes: 3 additions & 2 deletions m2cgen/assemblers/boosting.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ 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)
name: idx for idx, name in enumerate(feature_names or [])
}

model_dump = model.get_booster().get_dump(dump_format="json")
Expand All @@ -103,7 +103,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 = tree["split"]
feature_idx = self._feature_name_to_idx.get(split, split)
feature_ref = ast.FeatureRef(feature_idx)

# Since comparison with NaN (missing) value always returns false we
Expand Down
44 changes: 44 additions & 0 deletions tests/assemblers/test_xgboost.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,47 @@ def test_multi_class_best_ntree_limit():
])

assert utils.cmp_exprs(actual, expected)


def test_regression_saved_without_feature_names():
import os

base_score = 0.6
estimator = xgboost.XGBRegressor(n_estimators=2, random_state=1,
max_depth=1, base_score=base_score)
utils.train_model_regression(estimator)

filename = "tmp.file"
if os.path.exists(filename):
Copy link
Member

@izeigerman izeigerman Jul 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding the test!
May I please ask you to use tmp_dir utility function to avoid polluting user's workspace. Example can be found here:

with utils.tmp_dir() as tmp_dirpath:

Note that this context manager will clean up the temporary directory automatically so you don't have to do it manually here.
I'd also suggest to use UUID for a filename in place of hardcoded tmp.file to avoid potential conflicts, since this particular test should not be affected by artifacts which can potentially be produced by other tests.

Great job otherwise 👍

UPD: You can actually use any filename, since the entire tmp directory will be removed once this test is finished.

raise OSError("File is already exist")
estimator.save_model(filename)
estimator = xgboost.XGBRegressor(base_score=base_score)
estimator.load_model(filename)
if os.path.exists(filename):
os.remove(filename)

assembler = assemblers.XGBoostModelAssembler(estimator)
actual = assembler.assemble()

expected = ast.SubroutineExpr(
ast.BinNumExpr(
ast.BinNumExpr(
ast.NumVal(base_score),
ast.IfExpr(
ast.CompExpr(
ast.FeatureRef(12),
ast.NumVal(9.72500038),
ast.CompOpType.GTE),
ast.NumVal(1.67318344),
ast.NumVal(2.92757893)),
ast.BinNumOpType.ADD),
ast.IfExpr(
ast.CompExpr(
ast.FeatureRef(5),
ast.NumVal(6.94099998),
ast.CompOpType.GTE),
ast.NumVal(3.3400948),
ast.NumVal(1.72118247)),
ast.BinNumOpType.ADD))

assert utils.cmp_exprs(actual, expected)