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

Constructor Details

#initializeObject



35
36
37
38
39
40
41
# File 'lib/ai4r/classifiers/zero_r.rb', line 35

def initialize
  super()
  @default_class = nil
  @tie_break = :first
  @random_seed = nil
  @rng = nil
end

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.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ai4r/classifiers/zero_r.rb', line 48

def build(data_set)
  @data_set = data_set

  if @data_set.data_items.empty?
    @class_value = @default_class
    return self
  end

  frequencies = Hash.new(0)
  max_freq = 0
  tied_classes = []

  @data_set.data_items.each do |example|
    class_value = example.last
    frequencies[class_value] += 1
    class_frequency = frequencies[class_value]
    if class_frequency > max_freq
      max_freq = class_frequency
      tied_classes = [class_value]
    elsif class_frequency == max_freq && !tied_classes.include?(class_value)
      tied_classes << class_value
    end
  end

  rng = @rng || (@random_seed.nil? ? Random.new : Random.new(@random_seed))

  @class_value = if tied_classes.length == 1
                   tied_classes.first
                 else
                   case @tie_break
                   when :random
                     tied_classes.sample(random: rng)
                   else
                     tied_classes.first
                   end
                 end

  self
end

#eval(_data) ⇒ Object

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

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


93
94
95
# File 'lib/ai4r/classifiers/zero_r.rb', line 93

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'


109
110
111
# File 'lib/ai4r/classifiers/zero_r.rb', line 109

def get_rules
  "#{@data_set.category_label} = '#{@class_value}'"
end