-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogisticregression.rb
More file actions
73 lines (57 loc) · 1.83 KB
/
logisticregression.rb
File metadata and controls
73 lines (57 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
69
70
71
72
73
#require 'matrix'
#
def sigmoid(x)
1 / (1 + Math::E**(-x))
end
## we just multiply x by its theta value and adding, that will give the hypothesis value
def computeCost(inputX, inputY, theta)
cur_error = 0
for i in 0..(inputY.length - 1) do
hypothesis_val = get_hypothesis_val(inputX, inputY, theta, i)
y_val = inputY[i].to_f
puts "y val is " + y_val.to_s
puts "hypothesis val is " + hypothesis_val.to_s
if (y_val == 1.0)
cur_error += -Math.log(hypothesis_val)
else
cur_error += -Math.log(1 - hypothesis_val)
end
end
cur_error /= inputY.length
return cur_error
end
def get_hypothesis_val(inputX, inputY, theta, i)
sigmoid_input = inputX[2 * i].to_f * theta[1] + inputX[2 * i + 1].to_f * theta[2] + theta[0]
sig_val = sigmoid(sigmoid_input)
end
def logisticRegression(inputX, inputY, theta, num_iter, alpha)
for iter in 1..num_iter do
theta_sums = [5, -1, 0]
for i in 0..(inputY.length - 1) do
difference = get_hypothesis_val(inputX, inputY, theta, i) - inputY[i].to_f
theta_sums[0] += difference
theta_sums[1] += difference * inputX[2 * i].to_f
theta_sums[2] += difference * inputX[2 * i + 1].to_f
end
theta[0] -= (alpha / inputY.length) * theta_sums[0]
theta[1] -= (alpha / inputY.length) * theta_sums[1]
theta[2] -= (alpha / inputY.length) * theta_sums[2]
puts computeCost(inputX, inputY, theta)
end
return theta
end
inputX = []
inputY = []
File.open("ex2data1.txt", "r") do |infile|
while (line = infile.gets)
data_point = line.split(",")
inputX << data_point[0]
inputX << data_point[1]
inputY << data_point[2]
end
end
theta = [0, 0, 0]
puts computeCost(inputX, inputY, theta)
newTheta = logisticRegression(inputX, inputY, theta, 1500, 0.001)
#puts computeCost(inputX, inputY, newTheta)
puts sigmoid(0)