Class: Ai4r::Classifiers::OneR

Inherits:
Classifier show all
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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Data::Parameterizable

#get_parameters, included, #set_parameters

Instance Attribute Details

#data_setObject (readonly)

Returns the value of attribute data_set


25
26
27
# File 'lib/ai4r/classifiers/one_r.rb', line 25

def data_set
  @data_set
end

#ruleObject (readonly)

Returns the value of attribute rule


25
26
27
# File 'lib/ai4r/classifiers/one_r.rb', line 25

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.


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ai4r/classifiers/one_r.rb', line 30

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
  domains[1...-1].each_index do |attr_index|
    rule = build_rule(@data_set.data_items, attr_index, domains)
    @rule = rule if !@rule || rule[:correct] > @rule[:correct]
  end
  return self
end

#eval(data) ⇒ Object

You can evaluate new data, predicting its class. e.g.

classifier.eval(['New York',  '<30', 'F'])  # => 'Y'

51
52
53
54
55
# File 'lib/ai4r/classifiers/one_r.rb', line 51

def eval(data)
  return @zero_r.eval(data) if @zero_r
  attr_value = data[@rule[:attr_index]]
  return @rule[:rule][attr_value]
end

#get_rulesObject

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'

71
72
73
74
75
76
77
78
79
80
# File 'lib/ai4r/classifiers/one_r.rb', line 71

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.data_labels.last
  @rule[:rule].each_pair do |attr_value, class_value|
    sentences << "#{attr_label} == '#{attr_value}' then #{class_label} = '#{class_value}'"
  end
  return "if " + sentences.join("\nelsif ") + "\nend"        
end