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
Prev Previous commit
Next Next commit
Add tests for a model without feature names
  • Loading branch information
akhvorov committed Jul 29, 2019
commit e6e8938ff2b29ce7ef3f933521bd3d5b3e09cd7c
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)