Class: Ai4r::Classifiers::GradientBoosting

Inherits:
Classifier
  • Object
show all
Defined in:
lib/ai4r/classifiers/gradient_boosting.rb

Overview

Gradient boosting regressor using simple linear regression base learners.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Data::Parameterizable

#get_parameters, included, #set_parameters

Constructor Details

#initializeGradientBoosting

Returns a new instance of GradientBoosting.



23
24
25
26
27
# File 'lib/ai4r/classifiers/gradient_boosting.rb', line 23

def initialize
  super()
  @n_estimators = 10
  @learning_rate = 0.1
end

Instance Attribute Details

#initial_valueObject (readonly)

Returns the value of attribute initial_value.



21
22
23
# File 'lib/ai4r/classifiers/gradient_boosting.rb', line 21

def initial_value
  @initial_value
end

#learnersObject (readonly)

Returns the value of attribute learners.



21
22
23
# File 'lib/ai4r/classifiers/gradient_boosting.rb', line 21

def learners
  @learners
end

Instance Method Details

#build(data_set) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ai4r/classifiers/gradient_boosting.rb', line 29

def build(data_set)
  data_set.check_not_empty
  @learners = []
  targets = data_set.data_items.map(&:last)
  @initial_value = targets.sum.to_f / targets.length
  predictions = Array.new(targets.length, @initial_value)
  @n_estimators.times do
    residuals = targets.zip(predictions).map { |y, f| y - f }
    items = data_set.data_items.each_with_index.map do |item, idx|
      item[0...-1] + [residuals[idx]]
    end
    ds = Ai4r::Data::DataSet.new(data_items: items, data_labels: data_set.data_labels)
    learner = SimpleLinearRegression.new.build(ds)
    @learners << learner
    pred = items.map { |it| learner.eval(it[0...-1]) }
    predictions = predictions.zip(pred).map { |f, p| f + (@learning_rate * p) }
  end
  self
end

#eval(data) ⇒ Object

rubocop:enable Metrics/AbcSize



50
51
52
53
54
55
56
# File 'lib/ai4r/classifiers/gradient_boosting.rb', line 50

def eval(data)
  value = @initial_value
  @learners.each do |learner|
    value += @learning_rate * learner.eval(data)
  end
  value
end

#get_rulesObject



58
59
60
# File 'lib/ai4r/classifiers/gradient_boosting.rb', line 58

def get_rules
  'GradientBoosting does not support rule extraction.'
end