Class: Ai4r::Classifiers::Votes
- Inherits:
-
Object
- Object
- Ai4r::Classifiers::Votes
- Defined in:
- lib/ai4r/classifiers/votes.rb
Overview
Simple vote counter used by ensemble methods.
Instance Method Summary collapse
- #get_winner(tie_break = :last, rng: Random.new) ⇒ Object
- #increment_category(category) ⇒ Object
- #initialize ⇒ Object constructor
- #tally_for(category) ⇒ Object
Constructor Details
#initialize ⇒ Object
17 18 19 |
# File 'lib/ai4r/classifiers/votes.rb', line 17 def initialize self.tally_sheet = Hash.new(0) end |
Instance Method Details
#get_winner(tie_break = :last, rng: Random.new) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/ai4r/classifiers/votes.rb', line 35 def get_winner(tie_break = :last, rng: Random.new) n = 0 # used to create a stable sort of the tallys sorted_sheet = tally_sheet.sort_by do |_, score| n += 1 [score, n] end return nil if sorted_sheet.empty? if tie_break == :random max_score = sorted_sheet.last[1] tied = sorted_sheet.select { |_, score| score == max_score }.map(&:first) tied.sample(random: rng) else sorted_sheet.last.first end end |
#increment_category(category) ⇒ Object
23 24 25 |
# File 'lib/ai4r/classifiers/votes.rb', line 23 def increment_category(category) tally_sheet[category] += 1 end |
#tally_for(category) ⇒ Object
29 30 31 |
# File 'lib/ai4r/classifiers/votes.rb', line 29 def tally_for(category) tally_sheet[category] end |