Class: XGBoost::DMatrix

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/xgboost/dmatrix.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, label: nil, weight: nil, missing: Float::NAN) ⇒ DMatrix

Returns a new instance of DMatrix.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/xgboost/dmatrix.rb', line 7

def initialize(data, label: nil, weight: nil, missing: Float::NAN)
  if data.is_a?(::FFI::AutoPointer)
    @handle = data
    return
  end

  if matrix?(data)
    nrow = data.row_count
    ncol = data.column_count
    flat_data = data.to_a.flatten
  elsif daru?(data)
    nrow, ncol = data.shape
    flat_data = data.map_rows(&:to_a).flatten
    feature_names = data.each_vector.map(&:name)
    feature_types =
      data.each_vector.map(&:db_type).map do |v|
        case v
        when "INTEGER"
          "int"
        when "DOUBLE"
          "float"
        else
          raise Error, "Unknown feature type: #{v}"
        end
      end
  elsif numo?(data)
    nrow, ncol = data.shape
  elsif rover?(data)
    nrow, ncol = data.shape
    feature_names = data.keys
    feature_types =
      data.types.map do |_, v|
        v = v.to_s
        if v.start_with?("int") || v.start_with?("uint")
          "int"
        elsif v.start_with?("float")
          "float"
        else
          raise Error, "Unknown feature type: #{v}"
        end
      end
    data = data.to_numo
  else
    nrow = data.count
    ncol = data.first.count
    if !data.all? { |r| r.size == ncol }
      raise ArgumentError, "Rows have different sizes"
    end
    flat_data = data.flatten
  end

  c_data = ::FFI::MemoryPointer.new(:float, nrow * ncol)
  if numo?(data)
    c_data.write_bytes(data.cast_to(Numo::SFloat).to_string)
  else
    handle_missing(flat_data, missing)
    c_data.write_array_of_float(flat_data)
  end

  out = ::FFI::MemoryPointer.new(:pointer)
  check_call FFI.XGDMatrixCreateFromMat(c_data, nrow, ncol, missing, out)
  @handle = ::FFI::AutoPointer.new(out.read_pointer, FFI.method(:XGDMatrixFree))

  self.feature_names = feature_names if feature_names
  self.feature_types = feature_types if feature_types

  self.label = label if label
  self.weight = weight if weight
end

Instance Attribute Details

#handleObject (readonly)

Returns the value of attribute handle.



5
6
7
# File 'lib/xgboost/dmatrix.rb', line 5

def handle
  @handle
end

Instance Method Details

#data_split_modeObject



131
132
133
134
135
# File 'lib/xgboost/dmatrix.rb', line 131

def data_split_mode
  out = ::FFI::MemoryPointer.new(:uint64)
  check_call FFI.XGDMatrixDataSplitMode(handle, out)
  out.read_uint64 == 0 ? :row : :col
end

#feature_namesObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/xgboost/dmatrix.rb', line 147

def feature_names
  length = ::FFI::MemoryPointer.new(:uint64)
  sarr = ::FFI::MemoryPointer.new(:pointer)
  check_call(
    FFI.XGDMatrixGetStrFeatureInfo(
      handle,
      "feature_name",
      length,
      sarr
    )
  )
  feature_names = from_cstr_to_rbstr(sarr, length)
  feature_names.empty? ? nil : feature_names
end

#feature_names=(feature_names) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/xgboost/dmatrix.rb', line 162

def feature_names=(feature_names)
  if feature_names.nil?
    check_call(
      FFI.XGDMatrixSetStrFeatureInfo(
        handle, "feature_name", nil, 0
      )
    )
    return
  end

  # validate feature name
  feature_names =
    validate_feature_info(
      feature_names,
      num_col,
      data_split_mode == :col,
      "feature names"
    )
  if feature_names.length != feature_names.uniq.length
    raise ArgumentError, "feature_names must be unique"
  end

  # prohibit the use symbols that may affect parsing. e.g. []<
  if !feature_names.all? { |f| f.is_a?(String) && !["[", "]", "<"].any? { |x| f.include?(x) } }
    raise ArgumentError, "feature_names must be string, and may not contain [, ] or <"
  end

  c_feature_names = array_of_pointers(feature_names.map { |f| string_pointer(f) })
  check_call(
    FFI.XGDMatrixSetStrFeatureInfo(
      handle,
      "feature_name",
      c_feature_names,
      feature_names.length
    )
  )
end

#feature_typesObject



200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/xgboost/dmatrix.rb', line 200

def feature_types
  length = ::FFI::MemoryPointer.new(:uint64)
  sarr = ::FFI::MemoryPointer.new(:pointer)
  check_call(
    FFI.XGDMatrixGetStrFeatureInfo(
      handle,
      "feature_type",
      length,
      sarr
    )
  )
  res = from_cstr_to_rbstr(sarr, length)
  res.empty? ? nil : res
end

#feature_types=(feature_types) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/xgboost/dmatrix.rb', line 215

def feature_types=(feature_types)
  if feature_types.nil?
    check_call(
      FFI.XGDMatrixSetStrFeatureInfo(
        handle, "feature_type", nil, 0
      )
    )
    return
  end

  feature_types =
    validate_feature_info(
      feature_types,
      num_col,
      data_split_mode == :col,
      "feature types"
    )

  c_feature_types = array_of_pointers(feature_types.map { |f| string_pointer(f) })
  check_call(
    FFI.XGDMatrixSetStrFeatureInfo(
      handle,
      "feature_type",
      c_feature_types,
      feature_types.length
    )
  )
end

#groupObject



109
110
111
# File 'lib/xgboost/dmatrix.rb', line 109

def group
  uint_info("group_ptr")
end

#group=(group) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/xgboost/dmatrix.rb', line 89

def group=(group)
  c_data = ::FFI::MemoryPointer.new(:uint32, group.size)
  c_data.write_array_of_uint32(group)
  interface = {
    shape: [group.length],
    typestr: "|u4",
    data: [c_data.address, false],
    version: 3
  }
  check_call FFI.XGDMatrixSetInfoFromInterface(handle, "group", JSON.generate(interface))
end

#labelObject



101
102
103
# File 'lib/xgboost/dmatrix.rb', line 101

def label
  float_info("label")
end

#label=(label) ⇒ Object



81
82
83
# File 'lib/xgboost/dmatrix.rb', line 81

def label=(label)
  set_float_info("label", label)
end

#num_colObject



119
120
121
122
123
# File 'lib/xgboost/dmatrix.rb', line 119

def num_col
  out = ::FFI::MemoryPointer.new(:uint64)
  check_call FFI.XGDMatrixNumCol(handle, out)
  out.read_uint64
end

#num_nonmissingObject



125
126
127
128
129
# File 'lib/xgboost/dmatrix.rb', line 125

def num_nonmissing
  out = ::FFI::MemoryPointer.new(:uint64)
  check_call FFI.XGDMatrixNumNonMissing(handle, out)
  out.read_uint64
end

#num_rowObject



113
114
115
116
117
# File 'lib/xgboost/dmatrix.rb', line 113

def num_row
  out = ::FFI::MemoryPointer.new(:uint64)
  check_call FFI.XGDMatrixNumRow(handle, out)
  out.read_uint64
end

#save_binary(fname, silent: true) ⇒ Object



77
78
79
# File 'lib/xgboost/dmatrix.rb', line 77

def save_binary(fname, silent: true)
  check_call FFI.XGDMatrixSaveBinary(handle, fname, silent ? 1 : 0)
end

#slice(rindex) ⇒ Object



137
138
139
140
141
142
143
144
145
# File 'lib/xgboost/dmatrix.rb', line 137

def slice(rindex)
  idxset = ::FFI::MemoryPointer.new(:int, rindex.count)
  idxset.write_array_of_int(rindex)
  out = ::FFI::MemoryPointer.new(:pointer)
  check_call FFI.XGDMatrixSliceDMatrix(handle, idxset, rindex.size, out)

  handle = ::FFI::AutoPointer.new(out.read_pointer, FFI.method(:XGDMatrixFree))
  DMatrix.new(handle)
end

#weightObject



105
106
107
# File 'lib/xgboost/dmatrix.rb', line 105

def weight
  float_info("weight")
end

#weight=(weight) ⇒ Object



85
86
87
# File 'lib/xgboost/dmatrix.rb', line 85

def weight=(weight)
  set_float_info("weight", weight)
end