import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

 

from math import log

def logloss(p, y):
    epsilon = 10e-12
    if p == 0:
        p += epsilon
    if p == 1:
        p -= epsilon   
    if y == 1:
        return -log(p)   
    if y == 0:
        return -log(1-p)
    
def evaluate_logloss(p,labels):
    return sum(list(map(lambda x: logloss(p,x), labels)))/len(labels)

 

plt.figure(figsize = (15,5))

prob_i = np.linspace(0,1.,11)

label = np.concatenate((np.ones(5), np.zeros(5)))
logloss_i = [evaluate_logloss(p, label) for p in prob_i]
plt.subplot(131)
plt.plot(prob_i, logloss_i)
plt.axvline(.5, color='r')
plt.title('Equally weighted "0"/"1" cases')

label = np.concatenate((np.ones(3), np.zeros(7)))
logloss_i = [evaluate_logloss(p, label) for p in prob_i]

plt.subplot(132)
plt.plot(prob_i, logloss_i)
plt.title('70% of "0"')
plt.axvline(.3, color='r')

label = np.concatenate((np.ones(7), np.zeros(3)))
logloss_i = [evaluate_logloss(p, label) for p in prob_i]

plt.subplot(133)
plt.plot(prob_i, logloss_i)
plt.title('70% of "1"')
plt.axvline(.7, color='r');
Write a comment:

*

Your email address will not be published.

© 2014 In R we trust.
Top
Follow us: