多分类对数损失(Multi-Class Log-Loss)代码

代码: 

def multiclass_logloss(actual, predicted, eps=1e-15):
    """Logarithmic Loss  Metric
    :param actual: 包含actual target classes的数组
    :param predicted: 分类预测结果矩阵, 每个类别都有一个概率
    """
    # Convert 'actual' to a binary array if it's not already:
    if len(actual.shape) == 1:
        actual2 = np.zeros((actual.shape[0], predicted.shape[1]))
        for i, val in enumerate(actual):
            actual2[i, val] = 1
        actual = actual2

    clip = np.clip(predicted, eps, 1 - eps)
    rows = actual.shape[0]
    vsota = np.sum(actual * np.log(clip))
    return -1.0 / rows * vsota

解释: 

多分类对数损失(Multi-Class Log-Loss)代码_第1张图片

你可能感兴趣的:(NLP)