Skip to content

glassbox.metrics.classification

Classification metrics (accuracy, precision, recall, f1_score).


accuracy_score

accuracy_score(y_true, y_pred)

Compute the classification accuracy.

Parameters:

Name Type Description Default
y_true ndarray

Ground truth class labels of shape (n_samples,).

required
y_pred ndarray

Predicted class labels of shape (n_samples,).

required

Returns:

Type Description
float

Classification accuracy in the range [0.0, 1.0].

Source code in glassbox/metrics/classification.py
def accuracy_score(y_true: np.ndarray, y_pred: np.ndarray) -> float:
    """
    Compute the classification accuracy.

    Parameters
    ----------
    y_true : np.ndarray
        Ground truth class labels of shape (n_samples,).
    y_pred : np.ndarray
        Predicted class labels of shape (n_samples,).

    Returns
    -------
    float
        Classification accuracy in the range [0.0, 1.0].
    """
    y_true_arr, y_pred_arr = _validate_classification_inputs(y_true=y_true, y_pred=y_pred)
    return float(np.mean(y_true_arr == y_pred_arr))

precision_score

precision_score(y_true, y_pred)

Compute the classification precision score.

Parameters:

Name Type Description Default
y_true ndarray

Ground truth class labels of shape (n_samples,).

required
y_pred ndarray

Predicted class labels of shape (n_samples,).

required

Returns:

Type Description
float

Precision score in the range [0.0, 1.0].

Notes

Uses macro averaging over all classes. Classes with zero predicted samples contribute a precision of 0.0.

Source code in glassbox/metrics/classification.py
def precision_score(y_true: np.ndarray, y_pred: np.ndarray) -> float:
    """
    Compute the classification precision score.

    Parameters
    ----------
    y_true : np.ndarray
        Ground truth class labels of shape (n_samples,).
    y_pred : np.ndarray
        Predicted class labels of shape (n_samples,).

    Returns
    -------
    float
        Precision score in the range [0.0, 1.0].

    Notes
    -----
    Uses macro averaging over all classes. Classes with zero predicted samples
    contribute a precision of 0.0.
    """
    cm = confusion_matrix(y_true=y_true, y_pred=y_pred)
    true_positives = np.diag(cm).astype(float)
    predicted_positives = np.sum(cm, axis=0).astype(float)
    precision_per_class = _safe_divide(true_positives, predicted_positives)
    return float(np.mean(precision_per_class))

recall_score

recall_score(y_true, y_pred)

Compute the classification recall score.

Parameters:

Name Type Description Default
y_true ndarray

Ground truth class labels of shape (n_samples,).

required
y_pred ndarray

Predicted class labels of shape (n_samples,).

required

Returns:

Type Description
float

Recall score in the range [0.0, 1.0].

Notes

Uses macro averaging over all classes. Classes with zero true samples contribute a recall of 0.0.

Source code in glassbox/metrics/classification.py
def recall_score(y_true: np.ndarray, y_pred: np.ndarray) -> float:
    """
    Compute the classification recall score.

    Parameters
    ----------
    y_true : np.ndarray
        Ground truth class labels of shape (n_samples,).
    y_pred : np.ndarray
        Predicted class labels of shape (n_samples,).

    Returns
    -------
    float
        Recall score in the range [0.0, 1.0].

    Notes
    -----
    Uses macro averaging over all classes. Classes with zero true samples
    contribute a recall of 0.0.
    """
    cm = confusion_matrix(y_true=y_true, y_pred=y_pred)
    true_positives = np.diag(cm).astype(float)
    actual_positives = np.sum(cm, axis=1).astype(float)
    recall_per_class = _safe_divide(true_positives, actual_positives)
    return float(np.mean(recall_per_class))

f1_score

f1_score(y_true, y_pred)

Compute the F1 score for classification.

Parameters:

Name Type Description Default
y_true ndarray

Ground truth class labels of shape (n_samples,).

required
y_pred ndarray

Predicted class labels of shape (n_samples,).

required

Returns:

Type Description
float

F1 score in the range [0.0, 1.0].

Notes

Uses macro averaging over all classes with per-class zero-division handling.

Source code in glassbox/metrics/classification.py
def f1_score(y_true: np.ndarray, y_pred: np.ndarray) -> float:
    """
    Compute the F1 score for classification.

    Parameters
    ----------
    y_true : np.ndarray
        Ground truth class labels of shape (n_samples,).
    y_pred : np.ndarray
        Predicted class labels of shape (n_samples,).

    Returns
    -------
    float
        F1 score in the range [0.0, 1.0].

    Notes
    -----
    Uses macro averaging over all classes with per-class zero-division handling.
    """
    cm = confusion_matrix(y_true=y_true, y_pred=y_pred)

    true_positives = np.diag(cm).astype(float)
    predicted_positives = np.sum(cm, axis=0).astype(float)
    actual_positives = np.sum(cm, axis=1).astype(float)

    precision_per_class = _safe_divide(true_positives, predicted_positives)
    recall_per_class = _safe_divide(true_positives, actual_positives)

    f1_numerator = 2.0 * precision_per_class * recall_per_class
    f1_denominator = precision_per_class + recall_per_class
    f1_per_class = _safe_divide(f1_numerator, f1_denominator)
    return float(np.mean(f1_per_class))

confusion_matrix

confusion_matrix(y_true, y_pred)

Compute the confusion matrix for classification results.

Parameters:

Name Type Description Default
y_true ndarray

Ground truth class labels of shape (n_samples,).

required
y_pred ndarray

Predicted class labels of shape (n_samples,).

required

Returns:

Type Description
ndarray

Confusion matrix of shape (n_classes, n_classes).

Notes

Rows correspond to true labels and columns correspond to predicted labels. Class order follows sorted unique labels from the union of y_true and y_pred.

Source code in glassbox/metrics/classification.py
def confusion_matrix(y_true: np.ndarray, y_pred: np.ndarray) -> np.ndarray:
    """
    Compute the confusion matrix for classification results.

    Parameters
    ----------
    y_true : np.ndarray
        Ground truth class labels of shape (n_samples,).
    y_pred : np.ndarray
        Predicted class labels of shape (n_samples,).

    Returns
    -------
    np.ndarray
        Confusion matrix of shape (n_classes, n_classes).

    Notes
    -----
    Rows correspond to true labels and columns correspond to predicted labels.
    Class order follows sorted unique labels from the union of `y_true` and
    `y_pred`.
    """
    y_true_arr, y_pred_arr = _validate_classification_inputs(y_true=y_true, y_pred=y_pred)

    labels = np.unique(np.concatenate((y_true_arr, y_pred_arr)))
    label_to_index = {label: idx for idx, label in enumerate(labels)}

    matrix = np.zeros((labels.shape[0], labels.shape[0]), dtype=int)
    for true_label, pred_label in zip(y_true_arr, y_pred_arr):
        true_idx = label_to_index[true_label]
        pred_idx = label_to_index[pred_label]
        matrix[true_idx, pred_idx] += 1

    return matrix