DEV Community

kojix2
kojix2

Posted on

2 2

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! 🌟

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more