class NaiveBayes

this is lifted in whole from this article. i don't understand the maths and i don't want to www.sitepoint.com/machine-learning-ruby-naive-bayes-theorem/

Constants

YAML_PATH

:nocov:

Attributes

classes[R]
feature_set[R]
features[R]
num_classes[R]

Public Class Methods

new(path = YAML_PATH) click to toggle source
# File lib/spellr/key_tuner/naive_bayes.rb, line 18
def initialize(path = YAML_PATH)
  load_from_yaml(path)
  @key = {}
end

Public Instance Methods

class_probability(features, class_name) click to toggle source

this is where we compute the final naive Bayesian probability for a given set of features being a part of a given class.

# 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) click to toggle source
# 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) click to toggle source

multiply together the feature probabilities for all of the features in a class for given values

# 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) click to toggle source

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

# 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() click to toggle source
# File lib/spellr/key_tuner/naive_bayes.rb, line 55
def heuristic_weight
  @heuristic_weight ||= 10**Spellr.config.key_heuristic_weight
end
key?(string) click to toggle source
# 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) click to toggle source
# 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