16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/aprendizaje_maquina/clasificacion_logistica.rb', line 16
def train(iterations,alpha = nil,type_of_train)
case type_of_train
when 'Grad' then
@cost_history = []
for i in 0..iterations
x = @x * @theta
hx = x.map { |e| sigmoid(e) }
@theta = @theta - alpha / @m * @x.transpose * (hx - @y)
costo = 0
cost.to_a.map{ |e| costo = e }
@cost_history << ["iteracion: #{i}",costo]
end
@cost_history
"theta values => #{@theta} | cost => #{costo}"
when 'Newm' then
@cost_history = []
for i in 0..iterations
x = @x * @theta
hx = x.map { |e| sigmoid(e) }
uno_menos_hx = hx.map{ |e| (1-e) }
escalar = []
for u in 0...hx.size
escalar << hx[u] * uno_menos_hx[u]
end
gradiente = (1.0/@m) * @x.transpose * (hx - @y)
hessian = (1.0/@m) * @x.transpose * sumatoria(escalar) * @x
inversa = (1.0/hessian.det) * (hessian.adjugate)
@theta = @theta - inversa * gradiente
costo = 0
cost.to_a.map{ |e| costo = e }
@cost_history << ["iteracion: #{i}",costo]
end
@cost_history
"theta values => #{@theta} | cost => #{costo}"
when 'SGD' then
@cost_history = []
for i in 0..iterations
for i in 0..i
x = matrix(@x.to_a.map{|e| e.shuffle })*@theta
hx = x.map {|e| sigmoid(e) }
@theta = @theta - alpha / @m * @x.transpose * (hx - @y)
costo = 0
cost.to_a.map{|e| costo = e }
@cost_history << ["iteracion: #{i}",costo]
end
end
@cost_history
"theta values => #{@theta} | cost => #{costo}"
end
end
|