Class: AROW::Model
- Inherits:
-
Object
- Object
- AROW::Model
- Defined in:
- lib/arow.rb
Instance Method Summary collapse
-
#initialize(num_features, r = 0.1) ⇒ Model
constructor
A new instance of Model.
- #margin(features) ⇒ Object
- #predict(features) ⇒ Object
- #update(features, label) ⇒ Object
Constructor Details
#initialize(num_features, r = 0.1) ⇒ Model
Returns a new instance of Model.
5 6 7 8 9 10 11 |
# File 'lib/arow.rb', line 5 def initialize(num_features, r = 0.1) @num_features = num_features @r = r @means = Array.new(num_features, 0.0) @covariances = Array.new(num_features, 1.0) end |
Instance Method Details
#margin(features) ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/arow.rb', line 13 def margin(features) result = 0.0 features.each do |index, value| result += @means[index] * value end return result end |
#predict(features) ⇒ Object
23 24 25 |
# File 'lib/arow.rb', line 23 def predict(features) return margin(features) > 0 end |
#update(features, label) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/arow.rb', line 27 def update(features, label) margin = margin(features) label_value = label ? 1 : -1; return false if label_value * margin >= 1 confidence = 0.0 features.each do |index, value| confidence += @covariances[index] * value * value end beta = 1.0 / (confidence + @r) alpha = label_value * (1.0 - label_value * margin) * beta features.each do |index, value| v = @covariances[index] * value @means[index] += alpha * v @covariances[index] -= beta * v * v end return true end |