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

This commit is contained in:
Jiaming Yuan 2021-06-21 20:58:43 +08:00 committed by GitHub
parent bbfffb444d
commit da1ad798ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 1 deletions

View File

@ -2235,7 +2235,7 @@ class Booster(object):
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=''):

View File

@ -171,6 +171,11 @@ class TestBasic:
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')