Class: Classyfier::NaiveBayes::NaiveBayesClassifier
- Inherits:
-
Object
- Object
- Classyfier::NaiveBayes::NaiveBayesClassifier
- Defined in:
- lib/classyfier/naive_bayes.rb
Instance Attribute Summary collapse
-
#attribute_counts ⇒ Object
readonly
Returns the value of attribute attribute_counts.
-
#category_counts ⇒ Object
readonly
Returns the value of attribute category_counts.
-
#data_size ⇒ Object
readonly
Returns the value of attribute data_size.
Instance Method Summary collapse
- #category_scores(data_hash) ⇒ Object
- #classify(data_hash) ⇒ Object
-
#initialize(opts = {}) ⇒ NaiveBayesClassifier
constructor
A new instance of NaiveBayesClassifier.
- #train(data_hash, category) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ NaiveBayesClassifier
Returns a new instance of NaiveBayesClassifier.
7 8 9 10 11 12 13 14 |
# File 'lib/classyfier/naive_bayes.rb', line 7 def initialize(opts = {}) @data_size = 0 @attribute_counts = {} @category_counts = {} @laplacean_smoother = opts[:laplacean_smoother] || 0 @precision = opts[:precision] ? Float(opts[:precision]**10) : 100000.0 end |
Instance Attribute Details
#attribute_counts ⇒ Object (readonly)
Returns the value of attribute attribute_counts.
5 6 7 |
# File 'lib/classyfier/naive_bayes.rb', line 5 def attribute_counts @attribute_counts end |
#category_counts ⇒ Object (readonly)
Returns the value of attribute category_counts.
5 6 7 |
# File 'lib/classyfier/naive_bayes.rb', line 5 def category_counts @category_counts end |
#data_size ⇒ Object (readonly)
Returns the value of attribute data_size.
4 5 6 |
# File 'lib/classyfier/naive_bayes.rb', line 4 def data_size @data_size end |
Instance Method Details
#category_scores(data_hash) ⇒ Object
33 34 35 |
# File 'lib/classyfier/naive_bayes.rb', line 33 def category_scores(data_hash) category_scores = _category_scores(data_hash) end |
#classify(data_hash) ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/classyfier/naive_bayes.rb', line 24 def classify(data_hash) category_scores = _category_scores(data_hash) max = [:none, 0] category_scores.each_pair do |key, value| max = [key, value] if value > max[1] end return max end |
#train(data_hash, category) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/classyfier/naive_bayes.rb', line 16 def train(data_hash, category) @data_size += 1 @category_counts[category] ||= 0 @category_counts[category] += 1 _learn(data_hash, category) end |