Class: Ai4r::Classifiers::ZeroR

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

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Data::Parameterizable

#get_parameters, included, #set_parameters

Instance Attribute Details

#class_valueObject (readonly)

Returns the value of attribute class_value


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

def class_value
  @class_value
end

#data_setObject (readonly)

Returns the value of attribute data_set


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

def data_set
  @data_set
end

Instance Method Details

#build(data_set) ⇒ Object

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.


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

def build(data_set)
  data_set.check_not_empty
  @data_set = data_set
  frequencies = {}
  max_freq = 0
  @class_value = nil
  @data_set.data_items.each do |example|
    class_value = example.last
    frequencies[class_value] = frequencies[class_value].nil? ? 1 : frequencies[class_value] + 1
    class_frequency = frequencies[class_value]
    if max_freq < class_frequency
      max_freq = class_frequency
      @class_value = class_value
    end
  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
# File 'lib/ai4r/classifiers/zero_r.rb', line 51

def eval(data)
  @class_value
end

#get_rulesObject

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'

66
67
68
# File 'lib/ai4r/classifiers/zero_r.rb', line 66

def get_rules
  return "#{@data_set.data_labels.last} = '#{@class_value}'"
end