Skip to content

Commit 8d7245e

Browse files
authored
Update logistic_regression.py
1 parent 1584220 commit 8d7245e

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

machine_learning/logistic_regression.py

+17
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,19 @@ def sigmoid_function(z):
3131
def cost_function(h, y):
3232
return (-y * np.log(h) - (1 - y) * np.log(1 - h)).mean()
3333

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+
3439

3540
# here alpha is the learning rate, X is the feature matrix,y is the target matrix
3641

3742
def logistic_reg(
3843
alpha,
3944
X,
4045
y,
46+
num_steps,
4147
max_iterations=70000,
4248
):
4349
converged = False
@@ -55,6 +61,17 @@ def logistic_reg(
5561
J = cost_function(h, y)
5662

5763
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+
5875

5976
if iterations == max_iterations:
6077
print ('Maximum iterations exceeded!')

0 commit comments

Comments
 (0)