DICE得分是一种衡量分类模型性能的指标,具有良好的鲁棒性,其计算公式为:
其中,TP是真阳性,FP是假阳性,FN是假阴性。 TP = True Positives (真正例): 正确预测为正类的样本数量 FP = False Positives (假阳性): 错误预测为正类的负类样本数量 FN = False Negatives (假阴性): 错误预测为负类的正类样本数量
标准代码如下
def dice_score(y_true, y_pred):
intersection = np.logical_and(y_true, y_pred).sum()
true_sum = y_true.sum()
pred_sum = y_pred.sum()
if true_sum == 0 or pred_sum == 0:
return 0.0
dice = (2.0 * intersection) / (true_sum + pred_sum)
return round(float(dice), 3)
在sklearn库中也有对应的计算方法,可以使用metrics模块中的f1_score函数,因为在二分类问题中DICE系数与F1分数是等价的:
def dice_score(y_true, y_pred):
from sklearn.metrics import f1_score
dice = f1_score(y_true, y_pred)
return round(float(dice), 3)