Skip to content

Update with log_likelihood logistic_regression.py #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 12, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions machine_learning/logistic_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,19 @@ 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

def logistic_reg(
alpha,
X,
y,
num_steps,
max_iterations=70000,
):
converged = False
Expand All @@ -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!')
Expand Down