Class: Perceptron

Inherits:
Object
  • Object
show all
Includes:
Tools::ClassifierMethods, Tools::DataMethods
Defined in:
lib/rubyml/perceptron.rb

Overview

The multiclass perceptron class with customizable number of iterations and folds.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Tools::ClassifierMethods

#correct_count, #generate_folds, #generate_test_set, #generate_train_set, #handle_epsilon, #training_accuracy

Methods included from Tools::DataMethods

#bias_trick, #load_data, #mat_to_array, #plot, #plot_function, #separate_data

Constructor Details

#initialize(iterations = 100, folds = 5) ⇒ Perceptron

Returns a new instance of Perceptron.



11
12
13
14
15
16
17
# File 'lib/rubyml/perceptron.rb', line 11

def initialize(iterations = 100, folds = 5)
  @iterations = iterations
  @epsilon = nil
  @folds = folds
  @labels = []
  @weights = {}
end

Instance Attribute Details

#foldsObject (readonly)

Returns the value of attribute folds.



9
10
11
# File 'lib/rubyml/perceptron.rb', line 9

def folds
  @folds
end

#iterationsObject (readonly)

Returns the value of attribute iterations.



9
10
11
# File 'lib/rubyml/perceptron.rb', line 9

def iterations
  @iterations
end

#labelsObject (readonly)

Returns the value of attribute labels.



9
10
11
# File 'lib/rubyml/perceptron.rb', line 9

def labels
  @labels
end

#weightsObject (readonly)

Returns the value of attribute weights.



9
10
11
# File 'lib/rubyml/perceptron.rb', line 9

def weights
  @weights
end

Instance Method Details

#cold_startObject



60
61
62
63
# File 'lib/rubyml/perceptron.rb', line 60

def cold_start
  @labels = []
  @weights = {}
end

#fit(x, y, cs = true) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rubyml/perceptron.rb', line 29

def fit(x, y, cs = true)
  cold_start if cs
  setup_weights(y)
  @iterations.times do
    x.row_count.times do |r|
      clbl = get_best_guess(x, r)
      next unless y[r, 0] != clbl
      x.column_count.times { |c| update_weights(clbl, y[r, 0], c, x[r, c]) }
    end
  end
end

#get_best_guess(x, r) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rubyml/perceptron.rb', line 47

def get_best_guess(x, r)
  clbl, cmax = nil
  @labels.each do |lbl|
    csum = 0.0
    x.column_count.times { |c| csum += @weights[lbl][c] * x[r, c] }
    if cmax.nil? || cmax <= csum
      cmax = csum
      clbl = lbl
    end
  end
  clbl
end

#predict(x) ⇒ Object



41
42
43
44
45
# File 'lib/rubyml/perceptron.rb', line 41

def predict(x)
  preds = []
  x.row_count.times { |r| preds << get_best_guess(x, r) }
  Matrix.columns([preds])
end

#setup_weights(y) ⇒ Object



19
20
21
22
# File 'lib/rubyml/perceptron.rb', line 19

def setup_weights(y)
  @labels = mat_to_array(y).uniq { |e| e }
  @labels.each { |lbl| @weights[lbl] = Hash.new(0) }
end

#update_weights(guess, real, c, w) ⇒ Object



24
25
26
27
# File 'lib/rubyml/perceptron.rb', line 24

def update_weights(guess, real, c, w)
  @weights[guess][c] -= w
  @weights[real][c] += w
end