From da1ad798cabb6868be368bd262814cebcbf3d094 Mon Sep 17 00:00:00 2001 From: Jiaming Yuan Date: Mon, 21 Jun 2021 20:58:43 +0800 Subject: [PATCH] Convert numpy float to Python float in feat score. (#7047) --- python-package/xgboost/core.py | 2 +- tests/python/test_basic.py | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/python-package/xgboost/core.py b/python-package/xgboost/core.py index d348355ab8a7..3f18069242f6 100644 --- a/python-package/xgboost/core.py +++ b/python-package/xgboost/core.py @@ -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=''): diff --git a/tests/python/test_basic.py b/tests/python/test_basic.py index 971d69318096..bddb4458b4b9 100644 --- a/tests/python/test_basic.py +++ b/tests/python/test_basic.py @@ -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')