Class: ML::Learner::LogisticRegressionLearner
- Inherits:
-
Object
- Object
- ML::Learner::LogisticRegressionLearner
- Includes:
- LinearToolbox, Toolbox
- Defined in:
- lib/method/logistic_regression.rb
Overview
Implementing logistic regression
Instance Attribute Summary
Attributes included from LinearToolbox
Instance Method Summary collapse
-
#initialize(dim, eta = 0.01, model = :variate) ⇒ LogisticRegressionLearner
constructor
Intialize logistic regression.
-
#train!(data, iteration = 1000) ⇒ Object
Train with supervised data.
Methods included from LinearToolbox
Methods included from Toolbox
Constructor Details
#initialize(dim, eta = 0.01, model = :variate) ⇒ LogisticRegressionLearner
Intialize logistic regression
14 15 16 17 18 |
# File 'lib/method/logistic_regression.rb', line 14 def initialize dim, eta = 0.01, model = :variate @dim = dim @eta = eta @model = model end |
Instance Method Details
#train!(data, iteration = 1000) ⇒ Object
Train with supervised data
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/method/logistic_regression.rb', line 24 def train! data, iteration = 1000 self.current_vector = Matrix.column_vector(Array.new(@dim + 1, 0)) iteration.times do if @model == :variate n = (rand * data.size).to_i key = data.keys[n] self.current_vector -= gradiant(key, data[key]).map {|e| e * @eta } else sum = Matrix.column_vector(Array.new(@dim + 1, 0)) for key, value in data sum += gradiant key, value end self.current_vector -= sum.map {|e| e * @eta / data.size } end end end |