Sha256: 95a2b213c5cfb3f9b34057a1d5353ffe194b2c4afab290f2317b1b0fec67e183
Contents?: true
Size: 1.55 KB
Versions: 1
Compression:
Stored size: 1.55 KB
Contents
module AprendizajeMaquina class RegresionLineal attr_reader :m,:b, :ecuacion, :theta attr_accessor :x,:y def initialize(x,y) @x = x @y = y if @x.is_a?(Array) @n = @x.length elsif @x.is_a?(Matrix) @n = @x.column_count end end def encontrar_ecuacion if @x.is_a?(Array) && @y.is_a?(Array) @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) inversa = (1.to_f/(@x.transpose*@x).det)*((@x.transpose*@x).adjugate) @theta = inversa * (@x.transpose * @y) @theta else raise ArgumentError end end def hacer_prediccion(x_a_predecir) 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 end private 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 def al_cuadrado(array) iter = @n - 1 x_2 = [] for i in 0..iter x_2 << array[i]**2 end x_2 end def sumatoria(array) array.inject(0) { |elem1, elem2| elem1 + elem2 } end def media(array) sumatoria(array).to_f / array.length end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
aprendizaje_maquina-0.1.0 | lib/aprendizaje_maquina/regresion_lineal.rb |