Module: Omoikane::Evaluator

Defined in:
lib/omoikane/evaluator.rb

Class Method Summary collapse

Class Method Details

.cross_validate(dataset, k) ⇒ Object


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/omoikane/evaluator.rb', line 4

def cross_validate(dataset, k)
  partition_size = (dataset.size / k.to_f).floor

  partitions = (0 .. k-1).to_a.map do |i|
    if i == k-1
      (partition_size * i) .. dataset.size - 1
    else
      (partition_size * i) .. (partition_size * (i + 1) - 1)
    end
  end

  partitions.each_with_index do |partition, i|
    validation = dataset[partition]

    training = (partitions - [partition]).reduce([]) do |acc, part|
      acc + dataset[part]
    end

    # Let the classifier do its work...
    yield training, validation
  end
end

.f1_score(precision, recall) ⇒ Object

F1 Score.


28
29
30
# File 'lib/omoikane/evaluator.rb', line 28

def f1_score(precision, recall)
  (2 * (precision * recall)) / (precision + recall)
end

.matthews(tp, fn, fp, tn) ⇒ Object

Matthews Correlation Coefficient.


33
34
35
36
37
38
# File 'lib/omoikane/evaluator.rb', line 33

def matthews(tp, fn, fp, tn)
  den = tp * tn - fp * fn
  num = (tp + fp) * (tp + fn) * (tn + fp) * (tn + fn)

  den / Math.sqrt(num)
end