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.
21 22 23 24 |
# File 'lib/spellr/key_tuner/naive_bayes.rb', line 21 def initialize(path = YAML_PATH) load_from_yaml(path) @key = {} end |
Instance Attribute Details
#classes ⇒ Object (readonly)
Returns the value of attribute classes.
18 19 20 |
# File 'lib/spellr/key_tuner/naive_bayes.rb', line 18 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.
19 20 21 |
# File 'lib/spellr/key_tuner/naive_bayes.rb', line 19 def features @features end |
#num_classes ⇒ Object (readonly)
Returns the value of attribute num_classes.
17 18 19 |
# File 'lib/spellr/key_tuner/naive_bayes.rb', line 17 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.
64 65 66 67 68 69 |
# File 'lib/spellr/key_tuner/naive_bayes.rb', line 64 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
71 72 73 74 75 |
# File 'lib/spellr/key_tuner/naive_bayes.rb', line 71 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
52 53 54 55 56 |
# File 'lib/spellr/key_tuner/naive_bayes.rb', line 52 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
46 47 48 |
# File 'lib/spellr/key_tuner/naive_bayes.rb', line 46 def feature_probability(feature, value, class_name) Stats.gaussian_probability(value, **feature_set[class_name][feature]) end |
#heuristic_weight ⇒ Object
58 59 60 |
# File 'lib/spellr/key_tuner/naive_bayes.rb', line 58 def heuristic_weight @heuristic_weight ||= 10**Spellr.config.key_heuristic_weight end |
#key?(string) ⇒ Boolean
26 27 28 29 30 |
# File 'lib/spellr/key_tuner/naive_bayes.rb', line 26 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
32 33 34 35 36 37 38 39 |
# File 'lib/spellr/key_tuner/naive_bayes.rb', line 32 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 |