Class: ML::Learner::LogisticRegressionLearner

Inherits:
Object
  • Object
show all
Includes:
LinearToolbox, Toolbox
Defined in:
lib/method/logistic_regression.rb

Overview

Implementing logistic regression

Instance Attribute Summary

Attributes included from LinearToolbox

#current_vector

Instance Method Summary collapse

Methods included from LinearToolbox

#line, #predict

Methods included from Toolbox

#classify_error, #predict

Constructor Details

#initialize(dim, eta = 0.01, model = :variate) ⇒ LogisticRegressionLearner

Intialize logistic regression

Parameters:

  • dim (Integer)

    the input dimension

  • eta (Numeric) (defaults to: 0.01)

    the eta parameter

  • model (Symbol) (defaults to: :variate)

    the learning model, :variate for variating learning rate and :fixed for fixed learning rate



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

Parameters:

  • data (Hash)

    supervised input data (mapping from array to integer)

  • iteration (Integer) (defaults to: 1000)

    the number of the iterations



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