ankane / eps

Machine learning for Ruby
MIT License
652 stars 15 forks source link

LightGBM model summary raises ArgumentError #21

Closed timcraft closed 3 years ago

timcraft commented 3 years ago

Calling #summary on a LightGBM model raises a comparison of Array with Array failed (ArgumentError) exception, which is coming from the #sort_by call in Eps::LightGBM#_summary. I don't know how to best add test coverage to expose the issue in the tests, but I can demonstrate it using data from LightGBMTest:

test_data = [
  ["drv", 1029],
  ["class", 144],
  ["displ", 4322],
  ["year", 1126],
  ["cyl", 320],
  [["model", "4wd"], 364],
  [["model", "pickup"], 160],
  [["model", "2wd"], 72],
  [["model", "a4"], 0],
  [["model", "awd"], 0]]

test_data << ["foo", 0]

p test_data.sort_by { |k, v| [-v, k] }

ArgumentError will be raised if there are any string keys and array keys with the same value.

This is because strings can't be compared to arrays:

irb(main):001:0> ["model", "awd"] <=> "foo"
=> nil

Converting all the keys to strings using #display_field before the call to #sort_by fixes the issue, so does flattening the temporary arrays inside the #sort_by block.

ankane commented 3 years ago

Hey @timcraft, thanks for reporting and debugging the cause! Fixed on master.