Class: DecisionTree::Bagging

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Bagging.



377
378
379
380
# File 'lib/SelfModifiedDecisionTree.rb', line 377

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.



376
377
378
# File 'lib/SelfModifiedDecisionTree.rb', line 376

def classifiers
  @classifiers
end

Instance Method Details

#predict(test) ⇒ Object



390
391
392
393
394
395
396
397
398
399
# File 'lib/SelfModifiedDecisionTree.rb', line 390

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



382
383
384
385
386
387
388
# File 'lib/SelfModifiedDecisionTree.rb', line 382

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