Skip to content

brier_loss_multi

Calculates the multiclass Brier score.

Parameters:

Name Type Description Default
y ndarray

True labels for the validation data.

required
probs ndarray

Probability predictions for each class. For binary classification, this is the probability for the positive class. For multiclass, it is a 2D array with probabilities.

required

Returns:

Name Type Description
float float

The calculated multiclass Brier score.

Source code in periomod/training/_metrics.py
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def brier_loss_multi(y: np.ndarray, probs: np.ndarray) -> float:
    """Calculates the multiclass Brier score.

    Args:
        y (np.ndarray): True labels for the validation data.
        probs (np.ndarray): Probability predictions for each class.
            For binary classification, this is the probability for the positive class.
            For multiclass, it is a 2D array with probabilities.

    Returns:
        float: The calculated multiclass Brier score.
    """
    y_bin = label_binarize(y, classes=np.unique(y))
    g = y_bin.shape[1]
    return np.mean(
        [brier_score_loss(y_true=y_bin[:, i], y_proba=probs[:, i]) for i in range(g)]
    ) * (g / 2)