class Xgboost::Booster

Public Class Methods

finalize(pointer) click to toggle source
# File lib/xgboost/booster.rb, line 11
def self.finalize(pointer)
  proc { FFI.XGBoosterFree(pointer) }
end
new() click to toggle source
# File lib/xgboost/booster.rb, line 5
def initialize
  @handle = ::FFI::MemoryPointer.new(:pointer)
  FFI.XGBoosterCreate(nil, 0, @handle)
  ObjectSpace.define_finalizer(self, self.class.finalize(handle_pointer))
end

Public Instance Methods

load(path) click to toggle source
# File lib/xgboost/booster.rb, line 15
def load(path)
  FFI.XGBoosterLoadModel(handle_pointer, path)
end
predict(input, missing: Float::NAN) click to toggle source
# File lib/xgboost/booster.rb, line 23
def predict(input, missing: Float::NAN)
  raise TypeError unless input.is_a?(Array)

  unless input_2d = input.first.is_a?(Array)
    input = [input]
  end

  out_len = ::FFI::MemoryPointer.new(:ulong_long)
  out_result = ::FFI::MemoryPointer.new(:pointer)

  data = ::FFI::MemoryPointer.new(:float, input.count * input.first.count)
  data.put_array_of_float(0, input.flatten)

  dmatrix = ::FFI::MemoryPointer.new(:pointer)
  FFI.XGDMatrixCreateFromMat(data, input.count, input.first.count, missing, dmatrix)

  FFI.XGBoosterPredict(handle_pointer, dmatrix.read_pointer, 0, 0, out_len, out_result)

  out = out_result.read_pointer.read_array_of_float(out_len.read_ulong_long)

  input_2d ? out : out.first
ensure
  FFI.XGDMatrixFree(dmatrix.read_pointer) if dmatrix
end
save(path) click to toggle source
# File lib/xgboost/booster.rb, line 19
def save(path)
  FFI.XGBoosterSaveModel(handle_pointer, path)
end

Private Instance Methods

handle_pointer() click to toggle source
# File lib/xgboost/booster.rb, line 50
def handle_pointer
  @handle.read_pointer
end