Class: AprendizajeMaquina::RegresionLineal

Inherits:
Object
  • Object
show all
Defined in:
lib/aprendizaje_maquina/regresion_lineal.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y) ⇒ RegresionLineal

Returns a new instance of RegresionLineal.

[View source]

5
6
7
8
9
10
11
12
13
14
# 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

Instance Attribute Details

#bObject (readonly)

Returns the value of attribute b.


3
4
5
# File 'lib/aprendizaje_maquina/regresion_lineal.rb', line 3

def b
  @b
end

#ecuacionObject (readonly)

Returns the value of attribute ecuacion.


3
4
5
# File 'lib/aprendizaje_maquina/regresion_lineal.rb', line 3

def ecuacion
  @ecuacion
end

#mObject (readonly)

Returns the value of attribute m.


3
4
5
# File 'lib/aprendizaje_maquina/regresion_lineal.rb', line 3

def m
  @m
end

#thetaObject (readonly)

Returns the value of attribute theta.


3
4
5
# File 'lib/aprendizaje_maquina/regresion_lineal.rb', line 3

def theta
  @theta
end

Class Method Details

.deprecate(old_method, new_method) ⇒ Object

[View source]

52
53
54
55
56
57
# 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

Instance Method Details

#find_ecuationObject Also known as: train

[View source]

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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

#make_prediction(x_a_predecir) ⇒ Object Also known as: predict

[View source]

33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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