Class: Ai4r::Classifiers::Prism

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

Overview

Introduction

This is an implementation of the PRISM algorithm (Cendrowska, 1987) Given a set of preclassified examples, it builds a set of rules to predict the class of other instaces.

  1. Cendrowska (1987). PRISM: An algorithm for inducing modular rules.

International Journal of Man-Machine Studies. 27(4):349-370.

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


29
30
31
# File 'lib/ai4r/classifiers/prism.rb', line 29

def data_set
  @data_set
end

#rulesObject (readonly)

Returns the value of attribute rules


29
30
31
# File 'lib/ai4r/classifiers/prism.rb', line 29

def rules
  @rules
end

Instance Method Details

#build(data_set) ⇒ Object

Build a new Prism classifier. You must provide a DataSet instance as parameter. The last attribute of each item is considered as the item class.


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ai4r/classifiers/prism.rb', line 34

def build(data_set)
  data_set.check_not_empty
  @data_set = data_set
  domains = @data_set.build_domains
  instances = @data_set.data_items.collect {|data| data }
  @rules = []
  domains.last.each do |class_value|
    while(has_class_value(instances, class_value))
      rule = build_rule(class_value, instances)
      @rules << rule
      instances = instances.select {|data| !matches_conditions(data, rule[:conditions])}
    end
  end
  return self
end

#eval(instace) ⇒ Object

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

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

53
54
55
56
57
58
# File 'lib/ai4r/classifiers/prism.rb', line 53

def eval(instace)
  @rules.each do |rule|
    return rule[:class_value] if matches_conditions(instace, rule[:conditions])
  end
  return nil
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 == '>80' then marketing_target = 'Y'
 elsif city == 'Chicago' and age_range == '[30-50)' then marketing_target = 'Y'
 else marketing_target = 'N'
 end

It is a nice way to inspect induction results, and also to execute them:

age_range = '[30-50)'
city = 'New York'
eval(classifier.get_rules) 
puts marketing_target
 'Y'

76
77
78
79
80
81
82
83
84
# File 'lib/ai4r/classifiers/prism.rb', line 76

def get_rules
  out = "if #{join_terms(@rules.first)} then #{then_clause(@rules.first)}"
  @rules[1...-1].each do |rule| 
    out += "\nelsif #{join_terms(rule)} then #{then_clause(rule)}"
  end
  out += "\nelse #{then_clause(@rules.last)}" if @rules.size > 1
  out += "\nend"
  return out
end