From 24295f3d62315a0054c89fe4f3ca03ddad745f0c Mon Sep 17 00:00:00 2001 From: fis Date: Fri, 18 Jun 2021 16:55:49 +0800 Subject: [PATCH 1/2] Convert numpy float to Python float in feat score. --- python-package/xgboost/core.py | 2 +- 1 file changed, 1 insertion(+), 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=''): From e5c29c8bc7a5b7e8d9f0d37f78d6b3f89c2a0a78 Mon Sep 17 00:00:00 2001 From: fis Date: Fri, 18 Jun 2021 17:00:49 +0800 Subject: [PATCH 2/2] Test. --- tests/python/test_basic.py | 5 +++++ 1 file changed, 5 insertions(+) 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')