Calculate the Dice Score (Dice Coefficient) for binary classification. The Dice score is similar to the F1 score and measures overlap between predicted and actual positive labels.
def dice_score(y_true, y_pred):
tp = sum(1 for t, p in zip(y_true, y_pred) if t == 1 and p == 1)
fp = sum(1 for t, p in zip(y_true, y_pred) if t == 0 and p == 1)
fn = sum(1 for t, p in zip(y_true, y_pred) if t == 1 and p == 0)
denominator = 2 * tp + fp + fn
if denominator == 0:
return 1.0
return round((2 * tp) / denominator, 4)