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


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

def data_set
  @data_set
end

#pipesObject (readonly)

Returns the value of attribute pipes


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

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.


31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ai4r/classifiers/hyperpipes.rb', line 31

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'

In case of a tie, the last category should win: www.csee.wvu.edu/~timm/tmp/r7.pdf


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

def eval(data)
  votes = Votes.new
  @pipes.each do |category, pipe|
    pipe.each_with_index do |bounds, i|
      if data[i].is_a? Numeric
        votes.increment_category(category) if data[i]>=bounds[:min] && data[i]<=bounds[:max]
      else
        votes.increment_category(category) if bounds[data[i]]
      end
    end
  end
  return votes.get_winner
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'

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

def get_rules
  rules = []
  rules << "votes = Votes.new"
  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.increment_category('#{category}') "
      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.get_winner"
  return rules.join("\n")
end