lib/xgb/classifier.rb in xgb-0.1.1 vs lib/xgb/classifier.rb in xgb-0.1.2
- old
+ new
@@ -1,39 +1,39 @@
module Xgb
- class Classifier
- def initialize(max_depth: 3, learning_rate: 0.1, n_estimators: 100, objective: "binary:logistic", importance_type: "gain")
- @params = {
- max_depth: max_depth,
- objective: objective,
- learning_rate: learning_rate
- }
- @n_estimators = n_estimators
- @importance_type = importance_type
+ class Classifier < Model
+ def initialize(max_depth: 3, learning_rate: 0.1, n_estimators: 100, objective: "binary:logistic", importance_type: "gain", **options)
+ super
end
- def fit(x, y)
+ def fit(x, y, eval_set: nil, early_stopping_rounds: nil, verbose: true)
n_classes = y.uniq.size
params = @params.dup
if n_classes > 2
params[:objective] = "multi:softprob"
params[:num_class] = n_classes
end
dtrain = DMatrix.new(x, label: y)
- @booster = Xgb.train(params, dtrain, num_boost_round: @n_estimators)
+ evals = Array(eval_set).map.with_index { |v, i| [DMatrix.new(v[0], label: v[1]), "validation_#{i}"] }
+
+ @booster = Xgb.train(params, dtrain,
+ num_boost_round: @n_estimators,
+ early_stopping_rounds: early_stopping_rounds,
+ verbose_eval: verbose,
+ evals: evals
+ )
nil
end
def predict(data)
- dmat = DMatrix.new(data)
- y_pred = @booster.predict(dmat)
+ y_pred = super(data)
if y_pred.first.is_a?(Array)
# multiple classes
y_pred.map do |v|
- v.map.with_index.max_by { |v2, i| v2 }.last
+ v.map.with_index.max_by { |v2, _| v2 }.last
end
else
y_pred.map { |v| v > 0.5 ? 1 : 0 }
end
end
@@ -46,23 +46,8 @@
# multiple classes
y_pred
else
y_pred.map { |v| [1 - v, v] }
end
- end
-
- def save_model(fname)
- @booster.save_model(fname)
- end
-
- def load_model(fname)
- @booster = Booster.new(params: @params, model_file: fname)
- end
-
- def feature_importances
- score = @booster.score(importance_type: @importance_type)
- scores = @booster.feature_names.map { |k| score[k] || 0.0 }
- total = scores.sum.to_f
- scores.map { |s| s / total }
end
end
end