DEV Community

kojix2
kojix2

Posted on

LightGBM with Ruby

LightGBM is a powerful gradient boosting framework.

Requirements

  • LightGBM (Ruby binding)
    • lib_lightgbm.so, lib_lightgbm.dylib, lib_lightgbm.dll are included in Gem.
  • Red Datasets
    • MNIST datasets.
gem install red-datasets
gem install lightgbm
Enter fullscreen mode Exit fullscreen mode

Run

require 'lightgbm'
require 'datasets'

train_mnist = Datasets::MNIST.new(type: :train)
test_mnist  = Datasets::MNIST.new(type: :test)

train_x = train_mnist.map(&:pixels)
train_y = train_mnist.map(&:label)

test_x  = test_mnist.map(&:pixels)
test_y  = test_mnist.map(&:label)

params = {
  task: :train,
  boosting_type: :gbdt,
  objective: :multiclass,
  num_class: 10
}

train_set = LightGBM::Dataset.new(train_x, label: train_y)
booster = LightGBM.train(params, train_set)

result = booster.predict(test_x)
result.map! { |i| i.index(i.max) }
accuracy = test_y.zip(result).count { |i, j| i == j } / test_y.size.to_f
puts accuracy
Enter fullscreen mode Exit fullscreen mode

0.9727

Good!γ€€πŸŒŸ

Top comments (0)