-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for different prediction types #10
Closed
+126
−20
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module LightGBM | ||
# Macro definition of prediction type in C API of LightGBM | ||
C_API_PREDICT_NORMAL = 0 | ||
C_API_PREDICT_RAW_SCORE = 1 | ||
C_API_PREDICT_LEAF_INDEX = 2 | ||
C_API_PREDICT_CONTRIB = 3 | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
require_relative "test_helper" | ||
|
||
class BoosterTest < Minitest::Test | ||
def test_model_file | ||
def test_predict | ||
x_test = [[3.7, 1.2, 7.2, 9.0], [7.5, 0.5, 7.9, 0.0]] | ||
booster = LightGBM::Booster.new(model_file: "test/support/model.txt") | ||
y_pred = booster.predict(x_test) | ||
|
@@ -23,21 +23,6 @@ def test_model_from_string | |
assert_elements_in_delta [0.9823112229173586, 0.9583143724610858], y_pred.first(2) | ||
end | ||
|
||
def test_feature_importance | ||
assert_equal [280, 285, 335, 148], booster.feature_importance | ||
end | ||
|
||
def test_feature_name | ||
assert_equal ["x0", "x1", "x2", "x3"], booster.feature_name | ||
end | ||
|
||
def test_feature_importance_bad_importance_type | ||
error = assert_raises(LightGBM::Error) do | ||
booster.feature_importance(importance_type: "bad") | ||
end | ||
assert_includes error.message, "Unknown importance type" | ||
end | ||
|
||
def test_predict_hash | ||
pred = booster.predict({x0: 3.7, x1: 1.2, x2: 7.2, x3: 9.0}) | ||
assert_in_delta 0.9823112229173586, pred | ||
|
@@ -88,6 +73,68 @@ def test_predict_rover | |
end | ||
end | ||
|
||
def test_predict_type_leaf_index | ||
x_test = [[3.7, 1.2, 7.2, 9.0], [7.5, 0.5, 7.9, 0.0]] | ||
leaf_indexes = booster.predict(x_test, predict_type: LightGBM::C_API_PREDICT_LEAF_INDEX) | ||
assert_equal 200, leaf_indexes.count | ||
assert_equal 9.0, leaf_indexes.first | ||
assert_equal 7.0, leaf_indexes.last | ||
|
||
x_test = [3.7, 1.2, 7.2, 9.0] | ||
leaf_indexes = booster.predict(x_test, predict_type: LightGBM::C_API_PREDICT_LEAF_INDEX) | ||
assert_equal 100, leaf_indexes.count | ||
assert_equal 9.0, leaf_indexes.first | ||
assert_equal 10.0, leaf_indexes.last | ||
end | ||
|
||
def test_predict_type_contrib | ||
x_test = [[3.7, 1.2, 7.2, 9.0], [7.5, 0.5, 7.9, 0.0]] | ||
results = booster.predict(x_test, predict_type: LightGBM::C_API_PREDICT_CONTRIB) | ||
assert_equal 10, results.count | ||
|
||
# split results on num_features + 1 | ||
predictions = results.each_slice(5).to_a | ||
shap_values_1 = predictions.first[0..-2] | ||
ypred_1 = predictions.first[-1] | ||
assert_elements_in_delta [ | ||
-0.0733949225678886, -0.24289592050101766, 0.24183795683166504, 0.063430775771174 | ||
], shap_values_1 | ||
assert_in_delta (0.9933333333834246), ypred_1 | ||
|
||
shap_values_2 = predictions.last[0..-2] | ||
ypred_2 = predictions.last[-1] | ||
assert_elements_in_delta [ | ||
0.1094902954684793, -0.2810485083947154, 0.26691627597706397, -0.13037702397316747 | ||
], shap_values_2 | ||
assert_in_delta (0.9933333333834246), ypred_2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand why the last value (supposed to be the prediction result) is the same on each row, and why different from expected |
||
|
||
# single row | ||
x_test = [3.7, 1.2, 7.2, 9.0] | ||
results = booster.predict(x_test, predict_type: LightGBM::C_API_PREDICT_CONTRIB) | ||
assert_equal 5, results.count | ||
shap_values = results[0..-2] | ||
ypred = results[-1] | ||
assert_elements_in_delta [ | ||
-0.0733949225678886, -0.24289592050101766, 0.24183795683166504, 0.063430775771174 | ||
], shap_values | ||
assert_in_delta (0.9933333333834246), ypred | ||
end | ||
|
||
def test_feature_importance | ||
assert_equal [280, 285, 335, 148], booster.feature_importance | ||
end | ||
|
||
def test_feature_name | ||
assert_equal ["x0", "x1", "x2", "x3"], booster.feature_name | ||
end | ||
|
||
def test_feature_importance_bad_importance_type | ||
error = assert_raises(LightGBM::Error) do | ||
booster.feature_importance(importance_type: "bad") | ||
end | ||
assert_includes error.message, "Unknown importance type" | ||
end | ||
|
||
def test_model_to_string | ||
assert booster.model_to_string | ||
end | ||
|
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using a single argument instead of 3 boolean args, like Python API does.
https://lightgbm.readthedocs.io/en/stable/pythonapi/lightgbm.Booster.html#lightgbm.Booster.predict
https://github.com/microsoft/LightGBM/blob/e057ae08e6bf6c6c84f276a127423fb145ca5fdb/python-package/lightgbm/basic.py#L1079-L1081
@ankane
Let me know which you prefer. I don't really see the point of 3 booleans, since they are exclusive...