From 8d7245ee5be63f991256bc2657ee3918a9b5b286 Mon Sep 17 00:00:00 2001 From: Rakshit Parashar <34675136+rishu2403@users.noreply.github.com> Date: Fri, 12 Jul 2019 11:54:47 -0700 Subject: [PATCH] Update logistic_regression.py --- machine_learning/logistic_regression.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/machine_learning/logistic_regression.py b/machine_learning/logistic_regression.py index 71952e792e81..dd68bdea7838 100644 --- a/machine_learning/logistic_regression.py +++ b/machine_learning/logistic_regression.py @@ -31,6 +31,11 @@ def sigmoid_function(z): def cost_function(h, y): return (-y * np.log(h) - (1 - y) * np.log(1 - h)).mean() +def log_likelihood(X, Y, weights): + scores = np.dot(features, weights) + ll = np.sum( target*scores - np.log(1 + np.exp(scores)) ) + return ll + # here alpha is the learning rate, X is the feature matrix,y is the target matrix @@ -38,6 +43,7 @@ def logistic_reg( alpha, X, y, + num_steps, max_iterations=70000, ): converged = False @@ -55,6 +61,17 @@ def logistic_reg( J = cost_function(h, y) iterations += 1 # update iterations + + for step in xrange(num_steps): + scores = np.dot(X, weights) + predictions = sigmoid(scores) + + if step % 10000 == 0: + print log_likelihood(X,Y,weights) # Print log-likelihood every so often + + + return weights + if iterations == max_iterations: print ('Maximum iterations exceeded!')