Class: NaiveBayesRb::NaiveBayes
- Inherits:
-
Object
- Object
- NaiveBayesRb::NaiveBayes
- Includes:
- SavedModel
- Defined in:
- lib/naive_bayes_rb/naive_bayes.rb
Instance Attribute Summary collapse
-
#model ⇒ Object
Returns the value of attribute model.
Instance Method Summary collapse
- #accuracy(prediction, actual) ⇒ Object
- #dimension ⇒ Object
- #fit(data, target) ⇒ Object
-
#initialize ⇒ NaiveBayes
constructor
A new instance of NaiveBayes.
- #predict(data) ⇒ Object
Methods included from SavedModel
Constructor Details
#initialize ⇒ NaiveBayes
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
#model ⇒ Object
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 |
#dimension ⇒ Object
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 |