lib/xgb/booster.rb in xgb-0.1.1 vs lib/xgb/booster.rb in xgb-0.1.2
- old
+ new
@@ -3,18 +3,25 @@
attr_accessor :best_iteration, :feature_names
def initialize(params: nil, model_file: nil)
@handle = ::FFI::MemoryPointer.new(:pointer)
check_result FFI.XGBoosterCreate(nil, 0, @handle)
+ ObjectSpace.define_finalizer(self, self.class.finalize(handle_pointer))
+
if model_file
check_result FFI.XGBoosterLoadModel(handle_pointer, model_file)
end
self.best_iteration = 0
set_param(params)
end
+ def self.finalize(pointer)
+ # must use proc instead of stabby lambda
+ proc { FFI.XGBoosterFree(pointer) }
+ end
+
def update(dtrain, iteration)
check_result FFI.XGBoosterUpdateOneIter(handle_pointer, iteration, dtrain.handle_pointer)
end
def eval_set(evals, iteration)
@@ -41,14 +48,14 @@
end
end
def predict(data, ntree_limit: nil)
ntree_limit ||= 0
- out_len = ::FFI::MemoryPointer.new(:ulong)
+ out_len = ::FFI::MemoryPointer.new(:uint64)
out_result = ::FFI::MemoryPointer.new(:pointer)
check_result FFI.XGBoosterPredict(handle_pointer, data.handle_pointer, 0, ntree_limit, out_len, out_result)
- out = out_result.read_pointer.read_array_of_float(out_len.read_ulong)
+ out = out_result.read_pointer.read_array_of_float(out_len.read_uint64)
num_class = out.size / data.num_row
out = out.each_slice(num_class).to_a if num_class > 1
out
end
@@ -56,13 +63,13 @@
check_result FFI.XGBoosterSaveModel(handle_pointer, fname)
end
# returns an array of strings
def dump(fmap: "", with_stats: false, dump_format: "text")
- out_len = ::FFI::MemoryPointer.new(:ulong)
+ out_len = ::FFI::MemoryPointer.new(:uint64)
out_result = ::FFI::MemoryPointer.new(:pointer)
check_result FFI.XGBoosterDumpModelEx(handle_pointer, fmap, with_stats ? 1 : 0, dump_format, out_len, out_result)
- out_result.read_pointer.get_array_of_string(0, out_len.read_ulong)
+ out_result.read_pointer.get_array_of_string(0, out_len.read_uint64)
end
def dump_model(fout, fmap: "", with_stats: false, dump_format: "text")
ret = dump(fmap: fmap, with_stats: with_stats, dump_format: dump_format)
File.open(fout, "wb") do |f|