Class: RevisedBayes

Inherits:
Object
  • Object
show all
Defined in:
lib/SelfModifiedDecisionTree.rb

Overview

Modified Naive Bayes #

This is a modified form of naive bayes that eliminates marshall loading and unloading to prevent specific # types of deserialization based attacks. #

Instance Method Summary collapse

Constructor Details

#initialize(*klasses) ⇒ RevisedBayes

Returns a new instance of RevisedBayes.



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/SelfModifiedDecisionTree.rb', line 13

def initialize(*klasses)
  @features_count = {}

  @klass_count = {}

  @klasses = klasses
  
  klasses.each do |klass|
    @features_count[klass] = Hash.new(0.0)

    @klass_count[klass] = 0.0
  end
end

Instance Method Details

#classify(*features) ⇒ Object

P(Class | Item) = P(Item | Class) * P(Class)



38
39
40
41
42
43
44
45
46
# File 'lib/SelfModifiedDecisionTree.rb', line 38

def classify(*features)
  scores = {}

  @klasses.each do |klass|
    scores[klass] = (prob_of_item_given_a_class(features, klass) * prob_of_class(klass))
  end

  scores.sort {|a,b| b[1] <=> a[1]}[0]
end

#train(klass, *features) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/SelfModifiedDecisionTree.rb', line 27

def train(klass, *features)
  features.uniq.each do |feature|

    @features_count[klass][feature] += 1

  end

  @klass_count[klass] += 1
end