File tree 1 file changed +17
-0
lines changed
1 file changed +17
-0
lines changed Original file line number Diff line number Diff line change @@ -31,13 +31,19 @@ def sigmoid_function(z):
31
31
def cost_function (h , y ):
32
32
return (- y * np .log (h ) - (1 - y ) * np .log (1 - h )).mean ()
33
33
34
+ def log_likelihood (X , Y , weights ):
35
+ scores = np .dot (features , weights )
36
+ ll = np .sum ( target * scores - np .log (1 + np .exp (scores )) )
37
+ return ll
38
+
34
39
35
40
# here alpha is the learning rate, X is the feature matrix,y is the target matrix
36
41
37
42
def logistic_reg (
38
43
alpha ,
39
44
X ,
40
45
y ,
46
+ num_steps ,
41
47
max_iterations = 70000 ,
42
48
):
43
49
converged = False
@@ -55,6 +61,17 @@ def logistic_reg(
55
61
J = cost_function (h , y )
56
62
57
63
iterations += 1 # update iterations
64
+
65
+ for step in xrange (num_steps ):
66
+ scores = np .dot (X , weights )
67
+ predictions = sigmoid (scores )
68
+
69
+ if step % 10000 == 0 :
70
+ print log_likelihood (X ,Y ,weights ) # Print log-likelihood every so often
71
+
72
+
73
+ return weights
74
+
58
75
59
76
if iterations == max_iterations :
60
77
print ('Maximum iterations exceeded!' )
You can’t perform that action at this time.
0 commit comments