xgb 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e9ad2900e477bc8e8fa5aca236a49fb4d41d8ec28b3c975049b16023a89cfe60
4
- data.tar.gz: 3228781e80b95310a5c874b27fd69ab29ec875e79daf1bdbe99d09178c1ed2b2
3
+ metadata.gz: 295787749986ba153354f61845509079f470a5d11015c20cb89a5c58f04b133a
4
+ data.tar.gz: e111099514e9a9a48d51a5033214a3e897eeff209302e47e1f30d79c818833c5
5
5
  SHA512:
6
- metadata.gz: 228b317e43933701e2b9d3bdd0a1fc9c8781e64e2ec494b63a8be3efa5487350b022b64bcaffb4088e7e24c5698c39f23864248a87277e63d07ed7bd17573a09
7
- data.tar.gz: f8cb92f5f10b20bb7342ee126a75cfb44fc85b0bd01e941b2031ad5b957be9c5c6060b22737ac04584f51eb0ed41668431c849a0417484358496606abf3690de
6
+ metadata.gz: 803003072dafa5d2de97da7721042b94315b125f62f4f524f818923c5e5f8d3277624b146cedd7b88d9ca0769bec074e99717fb27ce56f5d2bf079ba627d52f7
7
+ data.tar.gz: b9b14da27b6f8c03415d770c31dd16fdd112922daa2af3022df8554301eb5e43d0e873f21c65da13799e7a62d832cadbabff50188783aad5b5e7a577a9334005
@@ -1,3 +1,8 @@
1
+ ## 0.3.1 (2020-04-16)
2
+
3
+ - Added `feature_names` and `feature_types` to `DMatrix`
4
+ - Added feature names to `dump`
5
+
1
6
  ## 0.3.0 (2020-02-19)
2
7
 
3
8
  - Updated XGBoost to 1.0.0
@@ -31,7 +31,8 @@ module XGBoost
31
31
  booster = Booster.new(params: params)
32
32
  num_feature = dtrain.num_col
33
33
  booster.set_param("num_feature", num_feature)
34
- booster.feature_names = num_feature.times.map { |i| "f#{i}" }
34
+ booster.feature_names = dtrain.feature_names
35
+ booster.feature_types = dtrain.feature_types
35
36
  evals ||= []
36
37
 
37
38
  if early_stopping_rounds
@@ -1,6 +1,6 @@
1
1
  module XGBoost
2
2
  class Booster
3
- attr_accessor :best_iteration, :feature_names
3
+ attr_accessor :best_iteration, :feature_names, :feature_types
4
4
 
5
5
  def initialize(params: nil, model_file: nil)
6
6
  @handle = ::FFI::MemoryPointer.new(:pointer)
@@ -25,11 +25,8 @@ module XGBoost
25
25
  end
26
26
 
27
27
  def eval_set(evals, iteration)
28
- dmats = ::FFI::MemoryPointer.new(:pointer, evals.size)
29
- dmats.write_array_of_pointer(evals.map { |v| v[0].handle_pointer })
30
-
31
- evnames = ::FFI::MemoryPointer.new(:pointer, evals.size)
32
- evnames.write_array_of_pointer(evals.map { |v| ::FFI::MemoryPointer.from_string(v[1]) })
28
+ dmats = array_of_pointers(evals.map { |v| v[0].handle_pointer })
29
+ evnames = array_of_pointers(evals.map { |v| string_pointer(v[1]) })
33
30
 
34
31
  out_result = ::FFI::MemoryPointer.new(:pointer)
35
32
 
@@ -67,7 +64,13 @@ module XGBoost
67
64
  def dump(fmap: "", with_stats: false, dump_format: "text")
68
65
  out_len = ::FFI::MemoryPointer.new(:uint64)
69
66
  out_result = ::FFI::MemoryPointer.new(:pointer)
70
- check_result FFI.XGBoosterDumpModelEx(handle_pointer, fmap, with_stats ? 1 : 0, dump_format, out_len, out_result)
67
+
68
+ names = feature_names || []
69
+ fnames = array_of_pointers(names.map { |fname| string_pointer(fname) })
70
+ ftypes = array_of_pointers(feature_types || Array.new(names.size, string_pointer("float")))
71
+
72
+ check_result FFI.XGBoosterDumpModelExWithFeatures(handle_pointer, names.size, fnames, ftypes, with_stats ? 1 : 0, dump_format, out_len, out_result)
73
+
71
74
  out_result.read_pointer.get_array_of_string(0, read_uint64(out_len))
72
75
  end
73
76
 
@@ -155,7 +158,7 @@ module XGBoost
155
158
  end
156
159
 
157
160
  def [](key_name)
158
- key = ::FFI::MemoryPointer.from_string(key_name)
161
+ key = string_pointer(key_name)
159
162
  success = ::FFI::MemoryPointer.new(:int)
160
163
  out_result = ::FFI::MemoryPointer.new(:pointer)
161
164
 
@@ -165,8 +168,8 @@ module XGBoost
165
168
  end
166
169
 
167
170
  def []=(key_name, raw_value)
168
- key = ::FFI::MemoryPointer.from_string(key_name)
169
- value = raw_value.nil? ? nil : ::FFI::MemoryPointer.from_string(raw_value)
171
+ key = string_pointer(key_name)
172
+ value = raw_value.nil? ? nil : string_pointer(raw_value)
170
173
 
171
174
  check_result FFI.XGBoosterSetAttr(handle_pointer, key, value)
172
175
  end
@@ -188,6 +191,14 @@ module XGBoost
188
191
  @handle.read_pointer
189
192
  end
190
193
 
194
+ def array_of_pointers(values)
195
+ ::FFI::MemoryPointer.new(:pointer, values.size).write_array_of_pointer(values)
196
+ end
197
+
198
+ def string_pointer(value)
199
+ ::FFI::MemoryPointer.from_string(value.to_s)
200
+ end
201
+
191
202
  include Utils
192
203
  end
193
204
  end
@@ -1,6 +1,6 @@
1
1
  module XGBoost
2
2
  class DMatrix
3
- attr_reader :data
3
+ attr_reader :data, :feature_names, :feature_types
4
4
 
5
5
  def initialize(data, label: nil, weight: nil, missing: Float::NAN)
6
6
  @data = data
@@ -15,6 +15,18 @@ module XGBoost
15
15
  elsif daru?(data)
16
16
  nrow, ncol = data.shape
17
17
  flat_data = data.map_rows(&:to_a).flatten
18
+ @feature_names = data.each_vector.map(&:name)
19
+ @feature_types =
20
+ data.each_vector.map(&:db_type).map do |v|
21
+ case v
22
+ when "INTEGER"
23
+ "int"
24
+ when "DOUBLE"
25
+ "float"
26
+ else
27
+ raise Error, "Unknown feature type: #{v}"
28
+ end
29
+ end
18
30
  elsif narray?(data)
19
31
  nrow, ncol = data.shape
20
32
  flat_data = data.flatten.to_a
@@ -30,6 +42,8 @@ module XGBoost
30
42
  check_result FFI.XGDMatrixCreateFromMat(c_data, nrow, ncol, missing, @handle)
31
43
 
32
44
  ObjectSpace.define_finalizer(self, self.class.finalize(handle_pointer))
45
+
46
+ @feature_names ||= ncol.times.map { |i| "f#{i}" }
33
47
  end
34
48
 
35
49
  self.label = label if label
@@ -31,7 +31,7 @@ module XGBoost
31
31
  attach_function :XGBoosterPredict, %i[pointer pointer int int int pointer pointer], :int
32
32
  attach_function :XGBoosterLoadModel, %i[pointer string], :int
33
33
  attach_function :XGBoosterSaveModel, %i[pointer string], :int
34
- attach_function :XGBoosterDumpModelEx, %i[pointer string int string pointer pointer], :int
34
+ attach_function :XGBoosterDumpModelExWithFeatures, %i[pointer int pointer pointer int string pointer pointer], :int
35
35
  attach_function :XGBoosterGetAttr, %i[pointer pointer pointer pointer], :int
36
36
  attach_function :XGBoosterSetAttr, %i[pointer pointer pointer], :int
37
37
  attach_function :XGBoosterGetAttrNames, %i[pointer pointer pointer], :int
@@ -1,3 +1,3 @@
1
1
  module XGBoost
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xgb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-02-20 00:00:00.000000000 Z
11
+ date: 2020-04-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ffi