lib/xgb/dmatrix.rb in xgb-0.1.2 vs lib/xgb/dmatrix.rb in xgb-0.1.3

- old
+ new

@@ -12,29 +12,30 @@ nrow = data.row_count ncol = data.column_count flat_data = data.to_a.flatten elsif daru?(data) nrow, ncol = data.shape - flat_data = data.each_vector.map(&:to_a).flatten + flat_data = data.map_rows(&:to_a).flatten elsif narray?(data) nrow, ncol = data.shape flat_data = data.flatten.to_a else nrow = data.count ncol = data.first.count flat_data = data.flatten end + handle_missing(flat_data, missing) c_data = ::FFI::MemoryPointer.new(:float, nrow * ncol) c_data.put_array_of_float(0, flat_data) check_result FFI.XGDMatrixCreateFromMat(c_data, nrow, ncol, missing, @handle) ObjectSpace.define_finalizer(self, self.class.finalize(handle_pointer)) end - set_float_info("label", label) if label - set_float_info("weight", weight) if weight + self.label = label if label + self.weight = weight if weight end def self.finalize(pointer) # must use proc instead of stabby lambda proc { FFI.XGDMatrixFree(pointer) } @@ -46,26 +47,34 @@ def weight float_info("weight") end + def label=(label) + set_float_info("label", label) + end + + def weight=(weight) + set_float_info("weight", weight) + end + def group=(group) c_data = ::FFI::MemoryPointer.new(:int, group.size) c_data.put_array_of_int(0, group) check_result FFI.XGDMatrixSetGroup(handle_pointer, c_data, group.size) end def num_row out = ::FFI::MemoryPointer.new(:uint64) check_result FFI.XGDMatrixNumRow(handle_pointer, out) - out.read_uint64 + read_uint64(out) end def num_col out = ::FFI::MemoryPointer.new(:uint64) check_result FFI.XGDMatrixNumCol(handle_pointer, out) - out.read_uint64 + read_uint64(out) end def slice(rindex) res = DMatrix.new(nil) idxset = ::FFI::MemoryPointer.new(:int, rindex.count) @@ -111,9 +120,13 @@ defined?(Daru::DataFrame) && data.is_a?(Daru::DataFrame) end def narray?(data) defined?(Numo::NArray) && data.is_a?(Numo::NArray) + end + + def handle_missing(data, missing) + data.map! { |v| v.nil? ? missing : v } end include Utils end end