Parent

Files

Ai4r::Classifiers::ZeroR

Introduction

The idea behind the ZeroR classifier is to identify the the most common class value in the training set. It always returns that value when evaluating an instance. It is frequently used as a baseline for evaluating other machine learning algorithms.

Attributes

class_value[R]
data_set[R]

Public Instance Methods

build(data_set) click to toggle source

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

# File lib/ai4r/classifiers/zero_r.rb, line 30
def build(data_set)
  data_set.check_not_empty
  @data_set = data_set
  frequence = {}
  max_freq = 0
  @class_value = nil
  @data_set.data_items.each do |example|
    class_value = example.last
    class_frequency = frequence[class_value]
    class_frequency = (class_frequency) ? class_frequency+1 : 1
    if max_freq < class_frequency
      max_freq = class_frequency
      @class_value = class_value
    end
  end
  return self
end
eval(data) click to toggle source

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

classifier.eval(['New York',  '<30', 'F'])  # => 'Y'
# File lib/ai4r/classifiers/zero_r.rb, line 51
def eval(data)
  @class_value
end
get_rules() click to toggle source

This method returns the generated rules in ruby code. e.g.

classifier.get_rules
  # =>  marketing_target='Y'

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'
# File lib/ai4r/classifiers/zero_r.rb, line 66
def get_rules
  return "#{@data_set.data_labels.last} = '#{@class_value}'"
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.