Class: PositronicBrain::Classifier::NaiveBayes

Inherits:
Base
  • Object
show all
Defined in:
lib/positronic_brain/classifier/naive_bayes.rb

Instance Attribute Summary

Attributes inherited from Base

#namespace, #persistence

Instance Method Summary collapse

Methods inherited from Base

#category_given_item_product, #extract_features, #initialize, #item_given_category_product, #normalized_prob_category_given_feature, #prob_category, #prob_category_given_feature, #prob_feature_given_category, #train

Methods inherited from Base

#dump_path, #initialize, initialize_persistence, #inspect, persistence, persistence_class, persistence_options

Constructor Details

This class inherits a constructor from PositronicBrain::Classifier::Base

Instance Method Details

#bayes_score(item, category) ⇒ Object Also known as: score



28
29
30
# File 'lib/positronic_brain/classifier/naive_bayes.rb', line 28

def bayes_score(item, category)
  item_given_category_product(item, category, weighted: true)*prob_category(category)
end

#bayes_scores(item) ⇒ Object Also known as: scores



20
21
22
23
24
25
# File 'lib/positronic_brain/classifier/naive_bayes.rb', line 20

def bayes_scores(item)
  categories.map do |category|
    score = bayes_score item, category
    [category, score]
  end
end

#classify(item, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/positronic_brain/classifier/naive_bayes.rb', line 4

def classify(item, options={})
  best_category = options[:default] || @default_category
  best_score    = options[:minimum] || @minimum_score || 0.0

  scores = scores item

  scores.each do |category, score|
    if score > best_score
      best_category = category
      best_score    = score
    end
  end

  [best_category, best_score]
end