Class: DecisionTree::Bagging
Instance Attribute Summary collapse
-
#classifiers ⇒ Object
Returns the value of attribute classifiers.
Instance Method Summary collapse
-
#initialize(attributes, data, default, type) ⇒ Bagging
constructor
A new instance of Bagging.
- #predict(test) ⇒ Object
- #train(data = @data, attributes = @attributes, default = @default) ⇒ Object
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
#classifiers ⇒ Object
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 |