class Callidus::Winnow
Attributes
a[RW]
input[RW]
mode[RW]
output[RW]
weights[R]
Public Class Methods
new(ip = [], op = [], options = {})
click to toggle source
# File lib/src/Winnow.rb, line 13 def initialize(ip = [], op = [], options = {}) @input = ip @output = op @default_weight = options[:default_weight] || 1 @a = options[:a] || 2; @mode = options[:mode] || 0 # 0 is demote, 1 is reset @weights = [] @trained = false end
Public Instance Methods
predict(x, options = {})
click to toggle source
# File lib/src/Winnow.rb, line 49 def predict(x, options = {}) assert_trained() bias = options[:bias] || 0 threshold = options[:threshold] || (@input.size / 2).to_i probability = 0; x.each_index do |i| probability += @weights[i] * x[i] + bias end (probability > threshold ? 1 : 0) end
train(a = @a)
click to toggle source
# File lib/src/Winnow.rb, line 31 def train(a = @a) if (@input.size > 0 and @input[0] and @input[0].size > 0 and @input[0].size != @weights.size) @weights = Array.new(@input[0].size, @default_weight) end @input.each_index do |i| @input[i].each_index do |j| if @input[i][j] == 1 @weights[j] *= @output[i] == 0 ? (@mode == 0 ? 1 / a : 0) : a end end end @trained = true self end
Private Instance Methods
assert_trained(method = "train")
click to toggle source
# File lib/src/Winnow.rb, line 25 def assert_trained(method = "train") if !@trained raise Util::UntrainedError.new("Winnow") end end