class AprendizajeMaquina::RegresionLineal
Attributes
b[R]
ecuacion[R]
m[R]
theta[R]
Public Class Methods
deprecate(old_method, new_method)
click to toggle source
# File lib/aprendizaje_maquina/regresion_lineal.rb, line 52 def self.deprecate(old_method, new_method) define_method(old_method) do |*args, &block| warn "Warning: #{old_method}() is deprecated. Use #{new_method}()." send(new_method, *args, &block) end end
new(x,y)
click to toggle source
# File lib/aprendizaje_maquina/regresion_lineal.rb, line 5 def initialize(x,y) @x = x @y = y @trained = false if @x.is_a?(Array) @n = @x.length elsif @x.is_a?(Matrix) @n = @x.column_count end end
Public Instance Methods
find_ecuation()
click to toggle source
# File lib/aprendizaje_maquina/regresion_lineal.rb, line 16 def find_ecuation if @x.is_a?(Array) && @y.is_a?(Array) @trained = true @m = ((@n*sumatoria(multiplicar(@x,@y))) - (sumatoria(@x)*sumatoria(@y))).to_f / ((@n*sumatoria(al_cuadrado(@x))) - (sumatoria(@x)**2)).to_f @b = media(@y) - (@m * media(@x)) @ecuacion = "Y = #{@m.round(4)}X+#{@b.round(4)}" @ecuacion elsif @x.is_a?(Matrix) && @y.is_a?(Vector) @trained = true inversa = (1.to_f/(@x.transpose*@x).det)*((@x.transpose*@x).adjugate) @theta = inversa * (@x.transpose * @y) @theta else raise ArgumentError end end
Also aliased as: train
make_prediction(x_a_predecir)
click to toggle source
# File lib/aprendizaje_maquina/regresion_lineal.rb, line 33 def make_prediction(x_a_predecir) if @trained == true if x_a_predecir.is_a?(Numeric) prediccion = (@m * x_a_predecir) + @b prediccion elsif x_a_predecir.is_a?(Matrix) prediccion = x_a_predecir * @theta prediccion else raise ArgumentError, "Must be a number or matrix 1xN" end else return "There is not a equation to make predictions (first, run find_ecuation method)" end end
Also aliased as: predict
Private Instance Methods
al_cuadrado(array)
click to toggle source
# File lib/aprendizaje_maquina/regresion_lineal.rb, line 73 def al_cuadrado(array) iter = @n - 1 x_2 = [] for i in 0..iter x_2 << array[i]**2 end x_2 end
media(array)
click to toggle source
# File lib/aprendizaje_maquina/regresion_lineal.rb, line 86 def media(array) sumatoria(array).to_f / array.length end
multiplicar(array_1,array_2)
click to toggle source
# File lib/aprendizaje_maquina/regresion_lineal.rb, line 64 def multiplicar(array_1,array_2) iter = @n - 1 xy = [] for i in 0..iter xy << array_1[i] * array_2[i] end xy end
sumatoria(array)
click to toggle source
# File lib/aprendizaje_maquina/regresion_lineal.rb, line 82 def sumatoria(array) array.inject(0) { |elem1, elem2| elem1 + elem2 } end