Class: NaiveBayes::Classifier
- Inherits:
-
Object
- Object
- NaiveBayes::Classifier
- Defined in:
- lib/naivebayes/classifier.rb
Instance Method Summary collapse
- #classify(attributes) ⇒ Object
-
#initialize(params) ⇒ Classifier
constructor
A new instance of Classifier.
- #train(label, attributes) ⇒ Object
Constructor Details
#initialize(params) ⇒ Classifier
Returns a new instance of Classifier.
6 7 8 9 10 11 12 |
# File 'lib/naivebayes/classifier.rb', line 6 def initialize(params) @frequency_table = Hash.new @word_table = Hash.new @instance_count_of = Hash.new(0) @total_count = 0 @model = params[:model] end |
Instance Method Details
#classify(attributes) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/naivebayes/classifier.rb', line 30 def classify(attributes) class_prior_of = Hash.new(1) likelihood_of = Hash.new(1) class_posterior_of = Hash.new(1) evidence = 0 @instance_count_of.each {|label, freq| class_prior_of[label] = freq.to_f / @total_count.to_f } @frequency_table.each_key {|label| likelihood_of[label] = 1 @word_table.each_key {|word| laplace_word_likelihood = (@frequency_table[label][word] + 1).to_f / (@instance_count_of[label] + @word_table.size()).to_f if attributes.has_key?(word) likelihood_of[label] *= laplace_word_likelihood else likelihood_of[label] *= (1 - laplace_word_likelihood) end } class_posterior_of[label] = class_prior_of[label] * likelihood_of[label] evidence += class_posterior_of[label] } class_posterior_of.each {|label, posterior| class_posterior_of[label] = posterior / evidence } return class_posterior_of end |
#train(label, attributes) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/naivebayes/classifier.rb', line 14 def train(label, attributes) unless @frequency_table.has_key?(label) @frequency_table[label] = Hash.new(0) end attributes.each {|word, frequency| if @model == "multinomial" @frequency_table[label][word] += frequency else @frequency_table[label][word] += 1 end @word_table[word] = 1 } @instance_count_of[label] += 1 @total_count += 1 end |