Class: Ai4r::Classifiers::Hyperpipes

Inherits:
Classifier show all
Defined in:
lib/ai4r/classifiers/hyperpipes.rb

Overview

Introduction

A fast classifier algorithm, created by Lucio de Souza Coelho and Len Trigg.

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/hyperpipes.rb', line 25

def data_set
  @data_set
end

#pipesObject (readonly)

Returns the value of attribute pipes.



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

def pipes
  @pipes
end

Instance Method Details

#build(data_set) ⇒ Object

Build a new Hyperpipes 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
# File 'lib/ai4r/classifiers/hyperpipes.rb', line 30

def build(data_set)
  data_set.check_not_empty
  @data_set = data_set
  @domains = data_set.build_domains
  
  @pipes = {}
  @domains.last.each {|cat| @pipes[cat] = build_pipe(@data_set)}
  @data_set.data_items.each {|item| update_pipe(@pipes[item.last], item) }
  
  return self
end

#eval(data) ⇒ Object

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

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


45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ai4r/classifiers/hyperpipes.rb', line 45

def eval(data)
  votes = Hash.new {0}
  @pipes.each do |category, pipe|
    pipe.each_with_index do |bounds, i|
      if data[i].is_a? Numeric
        votes[category]+=1 if data[i]>=bounds[:min] && data[i]<=bounds[:max]
      else
        votes[category]+=1 if bounds[data[i]]
      end
    end
  end
  return votes.to_a.max {|x, y| x.last <=> y.last}.first
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'


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ai4r/classifiers/hyperpipes.rb', line 73

def get_rules
  rules = []
  rules << "votes = Hash.new {0}"
  data = @data_set.data_items.first
  labels = @data_set.data_labels.collect {|l| l.to_s}
  @pipes.each do |category, pipe|
    pipe.each_with_index do |bounds, i|
      rule = "votes['#{category}'] += 1 "
      if data[i].is_a? Numeric
        rule += "if #{labels[i]} >= #{bounds[:min]} && #{labels[i]} <= #{bounds[:max]}"
      else
        rule += "if #{bounds.inspect}[#{labels[i]}]"
      end
      rules << rule
    end
  end
  rules << "#{labels.last} = votes.to_a.max {|x, y| x.last <=> y.last}.first"
  return rules.join("\n")
end