Class: Newral::Training::LinearRegressionMatrix

Inherits:
Object
  • Object
show all
Defined in:
lib/newral/training/linear_regression_matrix.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input: [], output: []) ⇒ LinearRegressionMatrix

Returns a new instance of LinearRegressionMatrix.



6
7
8
9
# File 'lib/newral/training/linear_regression_matrix.rb', line 6

def initialize( input: [], output: [] )
  @input = input
  @output = output
end

Instance Attribute Details

#best_errorObject (readonly)



5
6
7
# File 'lib/newral/training/linear_regression_matrix.rb', line 5

def best_error
  @best_error
end

#best_functionObject (readonly)



5
6
7
# File 'lib/newral/training/linear_regression_matrix.rb', line 5

def best_function
  @best_function
end

#inputObject (readonly)



5
6
7
# File 'lib/newral/training/linear_regression_matrix.rb', line 5

def input
  @input
end

Instance Method Details

#processObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/newral/training/linear_regression_matrix.rb', line 12

def process(  )
  # @input = [[6 ,4 ,11],[  8 ,5 ,15],[  12, 9, 25],[ 2, 1, 3]]
  # @output = [20,30,50,7]
  array = @input.collect do |input| 
     [1]+input
  end
  matrix = Matrix.rows array
  result = (( matrix.transpose*matrix ).inverse*(matrix.transpose)).to_a
  
  weights = result.collect do |r|
    r.size.times.inject(0) { |sum,i| sum = sum + r[i] * @output[i] }
  end
  bias = weights[0]
  @best_function = Newral::Functions::Vector.new vector: weights[1..weights.length-1], bias: bias 
  @best_error =@best_function.calculate_error( input: @input, output: @output )
  @best_function
end