Class: DecisionTree::Bagging

Inherits:
Object
  • Object
show all
Defined in:
lib/decisiontree/id3_tree.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes, data, default, type) ⇒ Bagging

Returns a new instance of Bagging.



303
304
305
306
# File 'lib/decisiontree/id3_tree.rb', line 303

def initialize(attributes, data, default, type)
  @classifiers, @type = [], type
  @data, @attributes, @default = data, attributes, default
end

Instance Attribute Details

#classifiersObject

Returns the value of attribute classifiers.



302
303
304
# File 'lib/decisiontree/id3_tree.rb', line 302

def classifiers
  @classifiers
end

Instance Method Details

#predict(test) ⇒ Object



316
317
318
319
320
321
322
323
324
325
# File 'lib/decisiontree/id3_tree.rb', line 316

def predict(test)
  predictions = Hash.new(0)
  @classifiers.each do |c|
    p, accuracy = c.predict(test)
    predictions[p] += accuracy unless p.nil?
  end
  return @default, 0.0 if predictions.empty?
  winner = predictions.sort_by {|k,v| -v}.first
  return winner[0], winner[1].to_f / @classifiers.size.to_f
end

#train(data = @data, attributes = @attributes, default = @default) ⇒ Object



308
309
310
311
312
313
314
# File 'lib/decisiontree/id3_tree.rb', line 308

def train(data=@data, attributes=@attributes, default=@default)
  @classifiers = []
  10.times { @classifiers << Ruleset.new(attributes, data, default, @type) }
  @classifiers.each do |c|
    c.train(data, attributes, default)
  end
end