Module: Bayes::Stats

Defined in:
lib/bayes/test.rb

Class Method Summary collapse

Class Method Details

.error_analysis(classifier, category, positive_items, negative_items) ⇒ Object

Error Analysis ====================================



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bayes/test.rb', line 8

def self.error_analysis(classifier, category, positive_items, negative_items)
  true_positives = 0
  true_negatives = 0
  false_negatives = 0
  false_positives = 0

  positive_items.each do |i|
    if classifier.classify(i) == category
      true_positives += 1.0
    else
      false_negatives += 1.0
    end
  end

  negative_items.each do |i|
    if classifier.classify(i) == category
      false_positives += 1.0
    else
      true_negatives += 1.0
    end
  end

  precision = true_positives / (true_positives + false_positives)
  recall = true_positives / (true_positives + false_negatives)
  f_score = 2 * ( (precision * recall) / (precision + recall) )

  {
    true_positives: true_positives,
    true_negatives: true_negatives,
    false_negatives: false_negatives,
    false_positives: false_positives,
    precision: precision,
    recall: recall,
    f_score: f_score,
  }
end

.error_analysis_csv(classifier, filename) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/bayes/test.rb', line 45

def self.error_analysis_csv(classifier, filename)
  items = File.read(filename).split("\n").map {|t| t.split("||") }

  correct = 0
  incorrect = 0

  items.each do |item|
    category = classifier.classify(item.first)
    if category == item.last
      correct += 1
    else
      incorrect += 1
    end
  end

  {
    correct: correct,
    incorrect: incorrect,
    error_rate: incorrect / (incorrect + correct).to_f
  }
end

.to_csv(results, name: "examples") ⇒ Object

Helpers ===================================================



69
70
71
72
73
74
75
76
77
78
# File 'lib/bayes/test.rb', line 69

def self.to_csv(results, name: "examples")
  `mkdir -p spec/reports`

  CSV.open("spec/reports/#{name}.csv", "w+") do |csv|
    csv << results.first.keys
    results.each do |r|
      csv << r.values
    end
  end
end