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

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay