[python-package] avoid data_has_header check in predict() #5970
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In the Python package, you can generate predictions on data stored in a delimited text file by passing a filepath to
Booster.predict()
.That calls
LGBM_BoosterPredictForFile()
in the C API, which takes a boolean argument indicating whether or not the file's first row is a header with feature names.That argument,
data_has_header
, is a boolean inBooster.predict()
's interface but an int inLGBM_BoosterPredictForFile()
, leading to this conversion:LightGBM/python-package/lightgbm/basic.py
Line 964 in 7140396
This PR proposes moving that conversion down into the
if-else
block corresponding to "data
is a file", so it's cost is avoided on all other prediction paths.I'm sure the cost of that one check is very very very very very small, but
Booster.predict()
is a latency-sensitive part of the API since it's used in model serving.