Module: Stamina::Classifier

Included in:
Automaton
Defined in:
lib/stamina-induction/stamina/classifier.rb

Overview

Provides a reusable module for binary classifiers. Classes including this module are required to provide a label_of(string) method, returning ‘1’ for strings considered positive, and ‘0’ fr strings considered negative.

Note that an Automaton being a classifier it already includes this module.

Instance Method Summary collapse

Instance Method Details

#correctly_classify?(sample) ⇒ Boolean

Checks if a labeled sample is correctly classified by the classifier.

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
# File 'lib/stamina-induction/stamina/classifier.rb', line 42

def correctly_classify?(sample)
  sample.each do |str|
    label = label_of(str)
    expected = (str.positive? ? '1' : '0')
    return false unless expected==label
  end
  true
end

#scoring(sample) ⇒ Object Also known as: classification_scoring

Classifies a sample then compute the classification scoring that is obtained by comparing the signature obtained by classification and the one of the sample itself. Returns an object responding to methods defined in Scoring module.

This method is actually a convenient shortcut for:

Stamina::Scoring.scoring(signature(sample), sample.signature)


34
35
36
# File 'lib/stamina-induction/stamina/classifier.rb', line 34

def scoring(sample)
  Stamina::Scoring.scoring(signature(sample), sample.signature)
end

#signature(sample) ⇒ Object Also known as: classification_signature

Computes a signature for a given sample (that is, an ordered set of strings). The signature is a string containing 1 (considered positive, or accepted) and 0 (considered negative, or rejected), one for each string.



16
17
18
19
20
21
22
# File 'lib/stamina-induction/stamina/classifier.rb', line 16

def signature(sample)
  signature = ''
  sample.each do |str|
    signature << label_of(str)
  end
  signature
end