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

Convert numpy float to Python float in feat score. #7047

Merged
merged 2 commits into from Jun 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion python-package/xgboost/core.py
Expand Up @@ -2235,7 +2235,7 @@ def get_score(
scores_arr = ctypes2numpy(scores, length.value, np.float32)
results = {}
for feat, score in zip(features_arr, scores_arr):
results[feat] = score
results[feat] = float(score)
return results

def trees_to_dataframe(self, fmap=''):
Expand Down
5 changes: 5 additions & 0 deletions tests/python/test_basic.py
Expand Up @@ -171,6 +171,11 @@ def test_feature_score(self):
with pytest.raises(ValueError):
booster.get_fscore()

booster.feature_names = None
# Use JSON to make sure the output has native Python type
scores = json.loads(json.dumps(booster.get_fscore()))
np.testing.assert_allclose(scores["f0"], 6.0)

def test_load_file_invalid(self):
with pytest.raises(xgb.core.XGBoostError):
xgb.Booster(model_file='incorrect_path')
Expand Down