Class: NaiveBayesRb::NaiveBayes

Inherits:
Object
  • Object
show all
Includes:
SavedModel
Defined in:
lib/naive_bayes_rb/naive_bayes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SavedModel

included, #save

Constructor Details

#initializeNaiveBayes

Returns a new instance of NaiveBayes.



7
8
9
# File 'lib/naive_bayes_rb/naive_bayes.rb', line 7

def initialize
  @model = {}
end

Instance Attribute Details

#modelObject

Returns the value of attribute model.



5
6
7
# File 'lib/naive_bayes_rb/naive_bayes.rb', line 5

def model
  @model
end

Instance Method Details

#accuracy(prediction, actual) ⇒ Object



26
27
28
# File 'lib/naive_bayes_rb/naive_bayes.rb', line 26

def accuracy(prediction, actual)
  Stats.accuracy(prediction, actual)
end

#dimensionObject



22
23
24
# File 'lib/naive_bayes_rb/naive_bayes.rb', line 22

def dimension
  (@model.values || []).length
end

#fit(data, target) ⇒ Object



11
12
13
14
15
16
# File 'lib/naive_bayes_rb/naive_bayes.rb', line 11

def fit(data, target)
  @model = data.zip(target)
               .group_by(&:last)
               .inject({}) { |h, (k, v)| h[k] = Stats.mean_stdev(v.map(&:first)); h}
  self
end

#predict(data) ⇒ Object



18
19
20
# File 'lib/naive_bayes_rb/naive_bayes.rb', line 18

def predict(data)
  data.map {|v| Stats.prediction(v, @model) }
end