Module: XGBoost

Defined in:
lib/xgboost.rb,
lib/xgboost/ffi.rb,
lib/xgboost/model.rb,
lib/xgboost/utils.rb,
lib/xgboost/ranker.rb,
lib/xgboost/booster.rb,
lib/xgboost/dmatrix.rb,
lib/xgboost/version.rb,
lib/xgboost/regressor.rb,
lib/xgboost/classifier.rb

Defined Under Namespace

Modules: FFI, Utils Classes: Booster, Classifier, DMatrix, Error, Model, Ranker, Regressor

Constant Summary collapse

VERSION =
"0.7.1"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.ffi_libObject

Returns the value of attribute ffi_lib.



20
21
22
# File 'lib/xgboost.rb', line 20

def ffi_lib
  @ffi_lib
end

Class Method Details

.cv(params, dtrain, num_boost_round: 10, nfold: 3, seed: 0, shuffle: true, verbose_eval: nil, show_stdv: true, early_stopping_rounds: nil) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/xgboost.rb', line 90

def cv(params, dtrain, num_boost_round: 10, nfold: 3, seed: 0, shuffle: true, verbose_eval: nil, show_stdv: true, early_stopping_rounds: nil)
  rand_idx = (0...dtrain.num_row).to_a
  rand_idx.shuffle!(random: Random.new(seed)) if shuffle

  kstep = (rand_idx.size / nfold.to_f).ceil
  test_id = rand_idx.each_slice(kstep).to_a[0...nfold]
  train_id = []
  nfold.times do |i|
    idx = test_id.dup
    idx.delete_at(i)
    train_id << idx.flatten
  end

  folds = train_id.zip(test_id)
  cvfolds = []
  folds.each do |(train_idx, test_idx)|
    fold_dtrain = dtrain.slice(train_idx)
    fold_dvalid = dtrain.slice(test_idx)
    booster = Booster.new(params: params)
    booster.set_param("num_feature", dtrain.num_col)
    cvfolds << [booster, fold_dtrain, fold_dvalid]
  end

  eval_hist = {}

  if early_stopping_rounds
    best_score = nil
    best_iter = nil
  end

  num_boost_round.times do |iteration|
    scores = {}

    cvfolds.each do |(booster, fold_dtrain, fold_dvalid)|
      booster.update(fold_dtrain, iteration)
      message = booster.eval_set([[fold_dtrain, "train"], [fold_dvalid, "test"]], iteration)

      res = message.split.map { |x| x.split(":") }[1..-1].map { |k, v| [k, v.to_f] }
      res.each do |k, v|
        (scores[k] ||= []) << v
      end
    end

    message_parts = ["[#{iteration}]"]

    last_mean = nil
    means = {}
    scores.each do |eval_name, vals|
      mean = mean(vals)
      stdev = stdev(vals)

      (eval_hist["#{eval_name}-mean"] ||= []) << mean
      (eval_hist["#{eval_name}-std"] ||= []) << stdev

      means[eval_name] = mean
      last_mean = mean

      if show_stdv
        message_parts << "%s:%g+%g" % [eval_name, mean, stdev]
      else
        message_parts << "%s:%g" % [eval_name, mean]
      end
    end

    if early_stopping_rounds
      score = last_mean
      # TODO handle larger better
      if best_score.nil? || score < best_score
        best_score = score
        best_iter = iteration
      elsif iteration - best_iter >= early_stopping_rounds
        eval_hist.each_key do |k|
          eval_hist[k] = eval_hist[k][0..best_iter]
        end
        break
      end
    end

    # put at end to keep output consistent with Python
    puts message_parts.join("\t") if verbose_eval
  end

  eval_hist
end

.lib_versionObject



175
176
177
178
179
180
181
# File 'lib/xgboost.rb', line 175

def lib_version
  major = ::FFI::MemoryPointer.new(:int)
  minor = ::FFI::MemoryPointer.new(:int)
  patch = ::FFI::MemoryPointer.new(:int)
  FFI.XGBoostVersion(major, minor, patch)
  "#{major.read_int}.#{minor.read_int}.#{patch.read_int}"
end

.train(params, dtrain, num_boost_round: 10, evals: nil, early_stopping_rounds: nil, verbose_eval: true) ⇒ Object



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
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/xgboost.rb', line 45

def train(params, dtrain, num_boost_round: 10, evals: nil, early_stopping_rounds: nil, verbose_eval: true)
  booster = Booster.new(params: params)
  num_feature = dtrain.num_col
  booster.set_param("num_feature", num_feature)
  booster.feature_names = dtrain.feature_names
  booster.feature_types = dtrain.feature_types
  evals ||= []

  if early_stopping_rounds
    best_score = nil
    best_iter = nil
    best_message = nil
  end

  num_boost_round.times do |iteration|
    booster.update(dtrain, iteration)

    if evals.any?
      message = booster.eval_set(evals, iteration)
      res = message.split.map { |x| x.split(":") }[1..-1].map { |k, v| [k, v.to_f] }

      if early_stopping_rounds && iteration == 0
        metric = res[-1][0]
        puts "Will train until #{metric} hasn't improved in #{early_stopping_rounds.to_i} rounds." if verbose_eval
      end

      puts message if verbose_eval
      score = res[-1][1]

      # TODO handle larger better
      if best_score.nil? || score < best_score
        best_score = score
        best_iter = iteration
        best_message = message
      elsif early_stopping_rounds && iteration - best_iter >= early_stopping_rounds
        booster.best_iteration = best_iter
        puts "Stopping. Best iteration:\n#{best_message}" if verbose_eval
        break
      end
    end
  end

  booster
end