Class: Ai4r::Experiment::ClassifierEvaluator
- Inherits:
-
Object
- Object
- Ai4r::Experiment::ClassifierEvaluator
- Defined in:
- lib/ai4r/experiment/classifier_evaluator.rb
Overview
The ClassifierEvaluator is useful to compare different classifiers algorithms. The evaluator builds the Classifiers using the same data examples, and provides methods to evalute their performance in parallel. It is a nice tool to compare and evaluate the performance of different algorithms, the same algorithm with different parameters, or your own new algorithm against the classic classifiers.
Instance Attribute Summary collapse
-
#build_times ⇒ Object
readonly
Returns the value of attribute build_times.
-
#classifiers ⇒ Object
readonly
Returns the value of attribute classifiers.
-
#eval_times ⇒ Object
readonly
Returns the value of attribute eval_times.
Instance Method Summary collapse
-
#add_classifier(classifier) ⇒ Object
(also: #<<)
Add a classifier instance to the test batch.
-
#build(data_set) ⇒ Object
Build all classifiers, using data examples found in data_set.
-
#eval(data) ⇒ Object
You can evaluate new data, predicting its class.
-
#initialize ⇒ ClassifierEvaluator
constructor
A new instance of ClassifierEvaluator.
-
#test(data_set) ⇒ Object
Test classifiers using a data set.
Constructor Details
#initialize ⇒ ClassifierEvaluator
Returns a new instance of ClassifierEvaluator.
19 20 21 |
# File 'lib/ai4r/experiment/classifier_evaluator.rb', line 19 def initialize @classifiers = [] end |
Instance Attribute Details
#build_times ⇒ Object (readonly)
Returns the value of attribute build_times.
17 18 19 |
# File 'lib/ai4r/experiment/classifier_evaluator.rb', line 17 def build_times @build_times end |
#classifiers ⇒ Object (readonly)
Returns the value of attribute classifiers.
17 18 19 |
# File 'lib/ai4r/experiment/classifier_evaluator.rb', line 17 def classifiers @classifiers end |
#eval_times ⇒ Object (readonly)
Returns the value of attribute eval_times.
17 18 19 |
# File 'lib/ai4r/experiment/classifier_evaluator.rb', line 17 def eval_times @eval_times end |
Instance Method Details
#add_classifier(classifier) ⇒ Object Also known as: <<
Add a classifier instance to the test batch
24 25 26 27 |
# File 'lib/ai4r/experiment/classifier_evaluator.rb', line 24 def add_classifier(classifier) @classifiers << classifier return self end |
#build(data_set) ⇒ Object
Build all classifiers, using data examples found in data_set. The last attribute of each item is considered as the item class. Building times are measured by separate, and can be accessed through build_times attribute reader.
36 37 38 39 40 41 42 |
# File 'lib/ai4r/experiment/classifier_evaluator.rb', line 36 def build(data_set) @build_times = [] @classifiers.each do |classifier| @build_times << Benchmark.measure { classifier.build data_set } end return self end |
#eval(data) ⇒ Object
You can evaluate new data, predicting its class. e.g.
classifier.eval(['New York', '<30', 'F'])
=> ['Y', 'Y', 'Y', 'N', 'Y', 'Y', 'N']
Evaluation times are measured by separate, and can be accessed through eval_times attribute reader.
50 51 52 53 54 55 56 57 |
# File 'lib/ai4r/experiment/classifier_evaluator.rb', line 50 def eval(data) @eval_times = [] results = [] @classifiers.each do |classifier| @eval_times << Benchmark.measure { results << classifier.eval(data) } end return results end |
#test(data_set) ⇒ Object
Test classifiers using a data set. The last attribute of each item is considered as the expected class. Data items are evaluated using all classifiers: evalution times, sucess rate, and quantity of classification errors are returned in a data set. The return data set has a row for every classifier tested, and the following attributes:
["Classifier", "Testing Time", "Errors", "Success rate"]
66 67 68 69 70 71 72 73 |
# File 'lib/ai4r/experiment/classifier_evaluator.rb', line 66 def test(data_set) result_data_items = [] @classifiers.each do |classifier| result_data_items << test_classifier(classifier, data_set) end return Ai4r::Data::DataSet.new(:data_items => result_data_items, :data_labels => ["Classifier","Testing Time","Errors","Success rate"]) end |