はじめに
LightGBMは、MicrosoftのGuolin Ke氏が中心になって開発している勾配ブースティングのフレームワークです。先日Ankane氏がRubyバインディングを公開しましたので、これを使ってMNIST手書き文字の識別をやってみます。
準備とインストール
-
LightGBM
- lightGBMはGemに内蔵されている。
-
Red Datasets
- Ruby から MNIST などのデータセットを呼び出す時に便利。
gem install lightgbm
gem install red-datasets
実行する
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
0.9727
短時間でかなりの精度が得られるようですね。