Class: ML::Learner::LinearRegressionLearner

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

Overview

Implementation of linear 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) ⇒ LinearRegressionLearner

Intialize linear regression

Parameters:

  • dim (Integer)

    the input dimension



13
14
15
# File 'lib/method/linear_regression.rb', line 13

def initialize dim
  @dim = dim
end

Instance Method Details

#train!(data) ⇒ Object

Train with supervised data

Parameters:

  • data (Hash)

    supervised input data (mapping from array to integer)



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/method/linear_regression.rb', line 20

def train! data
  x = Matrix.rows(data.keys)
  ary_y = []
  for k in data.keys
    ary_y << data[k]
  end
  y = Matrix.column_vector(ary_y)

  x_t = x.transpose
  x_dag = (x_t * x).inverse * x_t
  self.current_vector = x_dag * y
end