Class: Perceptron::Unit

Inherits:
Object
  • Object
show all
Defined in:
lib/perceptron/unit.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(features_number, weight = Weight.new(features_number)) ⇒ Unit

Returns a new instance of Unit.



6
7
8
# File 'lib/perceptron/unit.rb', line 6

def initialize(features_number, weight = Weight.new(features_number))
  @weight = weight
end

Instance Attribute Details

#weightObject (readonly)

Returns the value of attribute weight.



4
5
6
# File 'lib/perceptron/unit.rb', line 4

def weight
  @weight
end

Instance Method Details

#predict(vector) ⇒ Object



10
11
12
# File 'lib/perceptron/unit.rb', line 10

def predict(vector)
  scalar_product(@weight.vector, vector) > 0 ? 1 : 0
end

#train(hash) ⇒ Object



14
15
16
# File 'lib/perceptron/unit.rb', line 14

def train(hash)
  learn(hash) unless calculate_error(hash).zero?
end