Class: Newral::Training::LinearRegressionMatrix
- Inherits:
-
Object
- Object
- Newral::Training::LinearRegressionMatrix
- Defined in:
- lib/newral/training/linear_regression_matrix.rb
Instance Attribute Summary collapse
- #best_error ⇒ Object readonly
- #best_function ⇒ Object readonly
- #input ⇒ Object readonly
Instance Method Summary collapse
-
#initialize(input: [], output: []) ⇒ LinearRegressionMatrix
constructor
A new instance of LinearRegressionMatrix.
- #process ⇒ Object
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_error ⇒ Object (readonly)
5 6 7 |
# File 'lib/newral/training/linear_regression_matrix.rb', line 5 def best_error @best_error end |
#best_function ⇒ Object (readonly)
5 6 7 |
# File 'lib/newral/training/linear_regression_matrix.rb', line 5 def best_function @best_function end |
#input ⇒ Object (readonly)
5 6 7 |
# File 'lib/newral/training/linear_regression_matrix.rb', line 5 def input @input end |
Instance Method Details
#process ⇒ Object
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 |