Skip to content

Commit

Permalink
Improved handling of new and invalid categories
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Jan 5, 2025
1 parent 33f0369 commit 588ec93
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/lightgbm/inner_predictor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,17 @@ def cached_feature_name
def apply_pandas_categorical(data, categorical_feature, pandas_categorical)
(categorical_feature || []).each_with_index do |cf, i|
cat_codes = pandas_categorical[i].map.with_index.to_h
# TODO confirm column is categorical
data.each do |r|
# TODO decide how to handle missing values
r[cf] = cat_codes.fetch(r[cf])
cat = r[cf]
unless cat.nil?
r[cf] =
cat_codes.fetch(cat) do
unless cat.is_a?(String)
raise ArgumentError, "expected categorical value"
end
nil
end
end
end
end
end
Expand Down
18 changes: 18 additions & 0 deletions test/booster_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,24 @@ def test_predict_pandas_categorical_model_str
assert_elements_in_delta [0.996415541144579, 1.0809369939979934], y_pred.first(2)
end

def test_predict_pandas_categorical_missing_category
booster = LightGBM::Booster.new(model_file: "test/support/categorical.txt")
assert_in_delta 0.996415541144579, booster.predict([3.7, 1.2, 7.2, nil])
end

def test_predict_pandas_categorical_new_category
booster = LightGBM::Booster.new(model_file: "test/support/categorical.txt")
assert_in_delta 0.996415541144579, booster.predict([3.7, 1.2, 7.2, "cat10"])
end

def test_predict_pandas_categorical_invalid_category
booster = LightGBM::Booster.new(model_file: "test/support/categorical.txt")
error = assert_raises(ArgumentError) do
booster.predict([7.5, 0.5, 7.9, true])
end
assert_equal "expected categorical value", error.message
end

def test_model_to_string
assert booster.model_to_string
end
Expand Down

0 comments on commit 588ec93

Please sign in to comment.