Skip to content

Instantly share code, notes, and snippets.

@temmyzeus
Created October 26, 2020 02:43
Show Gist options
  • Save temmyzeus/0147ebcd28a9a983a3012f414876f4a7 to your computer and use it in GitHub Desktop.
Save temmyzeus/0147ebcd28a9a983a3012f414876f4a7 to your computer and use it in GitHub Desktop.
Implementation of Log Loss from scratch and comparism to Scikit-Learn Log Loss Metric
import numpy as np
from sklearn.metrics import log_loss
np.random.seed(0)#Setting our seed to make our results reproducable
#Creating a sample target and a sample predictions probabilites
targets = np.random.randint(low=0,high=2,size=100)
probs = np.random.rand(100)
#Using Scikit-Learn Log Loss Metric
sklearn_loss = log_loss(
y_true=targets,
y_pred=probs
)
print("Scikit Learn Loss = ",sklearn_loss)
#Implementation from Sratch
#Let's Create our custom log_loss metrics
def customLogLoss(label,probs):
if len(label) != len(probs):
print("Length of Target and Length of Preds are not same")
else:
losses = []
for yt,yp in zip(label,probs):
first_part = yt * np.log(yp)
second_part = (1-yt) * np.log(1-yp)
merge = -1 * (first_part + second_part)
losses.append(merge)
return np.mean(losses)#Taking the mean or average of the losses
custom_loss = customLogLoss(label=targets,probs=probs)
print("Custom Func Loss = ",custom_loss)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment