Skip to content

glassbox.metrics.regression

Regression metrics (MSE, MAE, R2).


mean_absolute_error

mean_absolute_error(y_true, y_pred)

Compute the mean absolute error (MAE).

Parameters:

Name Type Description Default
y_true ndarray

Ground truth target values of shape (n_samples,).

required
y_pred ndarray

Predicted target values of shape (n_samples,).

required

Returns:

Type Description
float

Mean absolute error.

Source code in glassbox/metrics/regression.py
def mean_absolute_error(y_true: np.ndarray, y_pred: np.ndarray) -> float:
    """
    Compute the mean absolute error (MAE).

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

    Returns
    -------
    float
        Mean absolute error.
    """
    y_true_arr, y_pred_arr = _validate_regression_inputs(y_true=y_true, y_pred=y_pred)
    return float(np.mean(np.abs(y_true_arr - y_pred_arr)))

mean_squared_error

mean_squared_error(y_true, y_pred)

Compute the mean squared error (MSE).

Parameters:

Name Type Description Default
y_true ndarray

Ground truth target values of shape (n_samples,).

required
y_pred ndarray

Predicted target values of shape (n_samples,).

required

Returns:

Type Description
float

Mean squared error.

Source code in glassbox/metrics/regression.py
def mean_squared_error(y_true: np.ndarray, y_pred: np.ndarray) -> float:
    """
    Compute the mean squared error (MSE).

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

    Returns
    -------
    float
        Mean squared error.
    """
    y_true_arr, y_pred_arr = _validate_regression_inputs(y_true=y_true, y_pred=y_pred)
    residuals = y_true_arr - y_pred_arr
    return float(np.mean(residuals**2))

r2_score

r2_score(y_true, y_pred)

Compute the coefficient of determination (R² score).

Parameters:

Name Type Description Default
y_true ndarray

Ground truth target values of shape (n_samples,).

required
y_pred ndarray

Predicted target values of shape (n_samples,).

required

Returns:

Type Description
float

R² score.

Notes

If y_true is constant, returns 1.0 for perfect predictions and 0.0 otherwise.

Source code in glassbox/metrics/regression.py
def r2_score(y_true: np.ndarray, y_pred: np.ndarray) -> float:
    """
    Compute the coefficient of determination (R² score).

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

    Returns
    -------
    float
        R² score.

    Notes
    -----
    If `y_true` is constant, returns 1.0 for perfect predictions and 0.0 otherwise.
    """
    y_true_arr, y_pred_arr = _validate_regression_inputs(y_true=y_true, y_pred=y_pred)

    ss_res = np.sum((y_true_arr - y_pred_arr) ** 2)
    y_mean = np.mean(y_true_arr)
    ss_tot = np.sum((y_true_arr - y_mean) ** 2)

    if ss_tot == 0.0:
        return 1.0 if ss_res == 0.0 else 0.0

    return float(1.0 - (ss_res / ss_tot))