binary の logloss と auc

Binary Class の測定

logloss

l_pred = [0.5, 0.5, 0.5, 0.5]
l_label = [0, 0, 0, 0]

def logloss(l_pred, l_label):
    n = len(l_pred)
    score = 0
    for t in range(n):
        i = l_pred[t]
        k = l_label[t]
        score +=  k * np.log(i) + (1 - k) * np.log(i)
    return - score / n

logloss(l_pred, l_label)

auc

from io import StringIO

a = StringIO('''target    prediction
1 0.38
0 0.51
1 0.90
1 0.84
1 0.48
0 0.03
0 0.45''')

def auc(df):
    df_ = df_ = df.sort_values('prediction', ascending=False).reset_index(drop=True)
    all_ = df_.target.value_counts()[0] * df_.target.value_counts()[1]
    
    target_list = df_.target.values
    n = len(target_list)
    count = 0
    for i in range(n):
        for k in range(i + 1, n):
            if target_list[i] < target_list[k]:
                count += 1
    return(1 - count / all_)
    
auc(df)