Skip to content

Commit

Permalink
Convert numpy float to Python float in feat score. (#7047)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivialfis committed Jun 21, 2021
1 parent bbfffb4 commit da1ad79
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
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

0 comments on commit da1ad79

Please sign in to comment.