Class: Ai4r::Classifiers::IB1

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

Overview

Introduction

IB1 algorithm implementation. IB1 is the simplest instance-based learning (IBL) algorithm.

  1. Aha, D. Kibler (1991). Instance-based learning algorithms.

Machine Learning. 6:37-66.

IBI is identical to the nearest neighbor algorithm except that it normalizes its attributes' ranges, processes instances incrementally, and has a simple policy for tolerating missing values

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Classifier

#get_rules

Methods included from Data::Parameterizable

#get_parameters, included, #set_parameters

Instance Attribute Details

#data_setObject (readonly)

Returns the value of attribute data_set


30
31
32
# File 'lib/ai4r/classifiers/ib1.rb', line 30

def data_set
  @data_set
end

Instance Method Details

#build(data_set) ⇒ Object

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


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

def build(data_set)
  data_set.check_not_empty
  @data_set = data_set
  @min_values = Array.new(data_set.data_labels.length)
  @max_values = Array.new(data_set.data_labels.length)
  data_set.data_items.each { |data_item| update_min_max(data_item[0...-1]) }
  return self
end

#eval(data) ⇒ Object

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

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

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

def eval(data)
  update_min_max(data)
  min_distance = 1.0/0
  klass = nil
  @data_set.data_items.each do |train_item|
    d = distance(data, train_item)
    if d < min_distance
      min_distance = d
      klass = train_item.last
    end
  end
  return klass
end