Class: Ai4r::Classifiers::OneR
- Inherits:
-
Classifier
- Object
- Classifier
- Ai4r::Classifiers::OneR
- Defined in:
- lib/ai4r/classifiers/one_r.rb
Overview
Introduction
The idea of the OneR algorithm is identify the single attribute to use to classify data that makes fewest prediction errors. It generates rules based on a single attribute. Numeric attributes are automatically discretized into a fixed number of bins (default is 10).
Instance Attribute Summary collapse
-
#data_set ⇒ Object
readonly
Returns the value of attribute data_set.
-
#rule ⇒ Object
readonly
Returns the value of attribute rule.
Instance Method Summary collapse
-
#build(data_set) ⇒ Object
Build a new OneR classifier.
-
#eval(data) ⇒ Object
You can evaluate new data, predicting its class.
-
#get_rules ⇒ Object
This method returns the generated rules in ruby code.
- #initialize ⇒ Object constructor
Methods included from Data::Parameterizable
#get_parameters, included, #set_parameters
Constructor Details
#initialize ⇒ Object
34 35 36 37 38 39 |
# File 'lib/ai4r/classifiers/one_r.rb', line 34 def initialize super() @selected_attribute = nil @tie_break = :first @bin_count = 10 end |
Instance Attribute Details
#data_set ⇒ Object (readonly)
Returns the value of attribute data_set.
27 28 29 |
# File 'lib/ai4r/classifiers/one_r.rb', line 27 def data_set @data_set end |
#rule ⇒ Object (readonly)
Returns the value of attribute rule.
27 28 29 |
# File 'lib/ai4r/classifiers/one_r.rb', line 27 def rule @rule end |
Instance Method Details
#build(data_set) ⇒ Object
Build a new OneR classifier. You must provide a DataSet instance as parameter. The last attribute of each item is considered as the item class.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/ai4r/classifiers/one_r.rb', line 46 def build(data_set) data_set.check_not_empty @data_set = data_set if data_set.num_attributes == 1 @zero_r = ZeroR.new.build(data_set) return self else @zero_r = nil end domains = @data_set.build_domains @rule = nil if @selected_attribute @rule = build_rule(@data_set.data_items, @selected_attribute, domains) else domains[1...-1].each_index do |attr_index| rule = build_rule(@data_set.data_items, attr_index, domains) if !@rule || rule[:correct] > @rule[:correct] || (rule[:correct] == @rule[:correct] && @tie_break == :last) @rule = rule end end end self end |
#eval(data) ⇒ Object
You can evaluate new data, predicting its class. e.g.
classifier.eval(['New York', '<30', 'F']) # => 'Y'
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/ai4r/classifiers/one_r.rb', line 76 def eval(data) return @zero_r.eval(data) if @zero_r attr_value = data[@rule[:attr_index]] if @rule[:bins] bin = @rule[:bins].find { |b| b.include?(attr_value) } attr_value = bin end @rule[:rule][attr_value] end |
#get_rules ⇒ Object
This method returns the generated rules in ruby code. e.g.
classifier.get_rules
# => if age_range == '<30' then marketing_target = 'Y'
elsif age_range == '[30-50)' then marketing_target = 'N'
elsif age_range == '[50-80]' then marketing_target = 'N'
end
It is a nice way to inspect induction results, and also to execute them:
marketing_target = nil
eval classifier.get_rules
puts marketing_target
# => 'Y'
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/ai4r/classifiers/one_r.rb', line 102 def get_rules return @zero_r.get_rules if @zero_r sentences = [] attr_label = @data_set.data_labels[@rule[:attr_index]] class_label = @data_set.category_label @rule[:rule].each_pair do |attr_value, class_value| sentences << if attr_value.is_a?(Range) "(#{attr_value}).include?(#{attr_label}) then #{class_label} = '#{class_value}'" else "#{attr_label} == '#{attr_value}' then #{class_label} = '#{class_value}'" end end "if #{sentences.join("\nelsif ")}\nend" end |