Skip to content

Instantly share code, notes, and snippets.

@PatrickRudgeri
Created July 13, 2020 19:42
Show Gist options
  • Save PatrickRudgeri/d2206eb798639500222c491b5373e843 to your computer and use it in GitHub Desktop.
Save PatrickRudgeri/d2206eb798639500222c491b5373e843 to your computer and use it in GitHub Desktop.
import numpy as np
def compute_log_loss(predicted, actual, eps=1e-14):
""" Computes the logarithmic loss between predicted and
actual when these are 1D arrays.
:param predicted: The predicted probabilities as floats between 0-1
:param actual: The actual binary labels. Either 0 or 1.
:param eps (optional): log(0) is inf, so we need to offset our
predicted values slightly by eps from 0 or 1.
"""
predicted = np.clip(predicted, eps, 1 - eps)
loss = -1 * np.mean(actual * np.log(predicted)
+ (1 - actual)
* np.log(1 - predicted))
return loss
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment