Class: NaiveBayes
- Inherits:
-
Object
- Object
- NaiveBayes
- Defined in:
- lib/spellr/key_tuner/naive_bayes.rb
Overview
Constant Summary collapse
- YAML_PATH =
:nocov:
File.join(__dir__, 'data.yml')
Instance Attribute Summary collapse
-
#classes ⇒ Object
readonly
Returns the value of attribute classes.
-
#feature_set ⇒ Object
readonly
Returns the value of attribute feature_set.
-
#features ⇒ Object
readonly
Returns the value of attribute features.
-
#num_classes ⇒ Object
readonly
Returns the value of attribute num_classes.
Instance Method Summary collapse
-
#class_probability(features, class_name) ⇒ Object
this is where we compute the final naive Bayesian probability for a given set of features being a part of a given class.
- #classify(features) ⇒ Object
-
#feature_multiplication(features, class_name) ⇒ Object
multiply together the feature probabilities for all of the features in a class for given values.
-
#feature_probability(feature, value, class_name) ⇒ Object
given a class, this method determines the probability of a certain value occurring for a given feature feature: name of the feature in consideration in the training data value: the value of the feature for which we are finding the probability class_name: name of the class in consideration.
- #heuristic_weight ⇒ Object
-
#initialize(path = YAML_PATH) ⇒ NaiveBayes
constructor
A new instance of NaiveBayes.
- #key?(string) ⇒ Boolean
- #load_from_yaml(path = YAML_PATH) ⇒ Object
Constructor Details
#initialize(path = YAML_PATH) ⇒ NaiveBayes
Returns a new instance of NaiveBayes.
18 19 20 21 |
# File 'lib/spellr/key_tuner/naive_bayes.rb', line 18 def initialize(path = YAML_PATH) load_from_yaml(path) @key = {} end |
Instance Attribute Details
#classes ⇒ Object (readonly)
Returns the value of attribute classes.
16 17 18 |
# File 'lib/spellr/key_tuner/naive_bayes.rb', line 16 def classes @classes end |
#feature_set ⇒ Object (readonly)
Returns the value of attribute feature_set.
16 17 18 |
# File 'lib/spellr/key_tuner/naive_bayes.rb', line 16 def feature_set @feature_set end |
#features ⇒ Object (readonly)
Returns the value of attribute features.
16 17 18 |
# File 'lib/spellr/key_tuner/naive_bayes.rb', line 16 def features @features end |
#num_classes ⇒ Object (readonly)
Returns the value of attribute num_classes.
16 17 18 |
# File 'lib/spellr/key_tuner/naive_bayes.rb', line 16 def num_classes @num_classes end |
Instance Method Details
#class_probability(features, class_name) ⇒ Object
this is where we compute the final naive Bayesian probability for a given set of features being a part of a given class.
61 62 63 64 65 66 |
# File 'lib/spellr/key_tuner/naive_bayes.rb', line 61 def class_probability(features, class_name) class_fraction = 1.0 / num_classes feature_bayes = feature_multiplication(features, class_name) feature_bayes *= heuristic_weight if class_name.start_with?('key_') feature_bayes * class_fraction end |
#classify(features) ⇒ Object
68 69 70 71 72 |
# File 'lib/spellr/key_tuner/naive_bayes.rb', line 68 def classify(features) classes.max_by do |class_name| class_probability(features, class_name) end end |
#feature_multiplication(features, class_name) ⇒ Object
multiply together the feature probabilities for all of the features in a class for given values
49 50 51 52 53 |
# File 'lib/spellr/key_tuner/naive_bayes.rb', line 49 def feature_multiplication(features, class_name) features.reduce(1.0) do |result, (key, value)| result * feature_probability(key, value, class_name) end end |
#feature_probability(feature, value, class_name) ⇒ Object
given a class, this method determines the probability of a certain value occurring for a given feature feature: name of the feature in consideration in the training data value: the value of the feature for which we are finding the probability class_name: name of the class in consideration
43 44 45 |
# File 'lib/spellr/key_tuner/naive_bayes.rb', line 43 def feature_probability(feature, value, class_name) Stats.gaussian_probability(value, **feature_set[class_name][feature]) end |
#heuristic_weight ⇒ Object
55 56 57 |
# File 'lib/spellr/key_tuner/naive_bayes.rb', line 55 def heuristic_weight @heuristic_weight ||= 10**Spellr.config.key_heuristic_weight end |
#key?(string) ⇒ Boolean
23 24 25 26 27 |
# File 'lib/spellr/key_tuner/naive_bayes.rb', line 23 def key?(string) @key.fetch(string) do @key[string] = classify(PossibleKey.new(string).features).start_with?('key') end end |
#load_from_yaml(path = YAML_PATH) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/spellr/key_tuner/naive_bayes.rb', line 29 def load_from_yaml(path = YAML_PATH) data = YAML.safe_load(::File.read(path), permitted_classes: [Symbol]) @feature_set = data[:feature_set] @num_classes = data[:num_classes] @classes = data[:classes] @features = data[:features] end |