Skip to content

Metrics

Evaluation metrics for highFIS estimators.

This module provides a small, sklearn-style evaluation API for both regression and classification tasks.

Classification Metrics
  • accuracy: standard accuracy score
  • balanced_accuracy: average recall over classes
  • precision_macro: macro-averaged precision
  • recall_macro: macro-averaged recall
  • f1_macro: macro-averaged F1 score
  • precision_micro: micro-averaged precision
  • recall_micro: micro-averaged recall
  • f1_micro: micro-averaged F1 score
  • confusion_matrix: confusion matrix by class
  • classes: sorted union of true and predicted labels
Regression Metrics
  • mse: mean squared error
  • mae: mean absolute error
  • rmse: root mean squared error
  • r2: coefficient of determination
  • median_absolute_error: median absolute error
  • mean_bias_error: average prediction bias
  • max_error: maximum absolute error
  • std_error: standard deviation of residuals
  • explained_variance: explained variance score
  • mape: mean absolute percentage error
  • smape: symmetric mean absolute percentage error
  • msle: mean squared logarithmic error
  • pearson: Pearson correlation coefficient
Notes
  • The module exports compute_metrics and the helper classes ClassificationMetrics and RegressionMetrics.
  • compute_metrics validates metric names and returns only the requested subset.
  • All metrics accept raw array-like inputs and flatten non-1D arrays.

ClassificationMetrics

Standard classification metrics.

accuracy staticmethod

Return classification accuracy.

Source code in highfis/metrics.py
@staticmethod
def accuracy(y_true: Any, y_pred: Any, sample_weight: Any | None = None) -> float:
    """Return classification accuracy."""
    return float(accuracy_score(y_true, y_pred, sample_weight=sample_weight))

balanced_accuracy staticmethod

Return the balanced accuracy score.

Source code in highfis/metrics.py
@staticmethod
def balanced_accuracy(y_true: Any, y_pred: Any, sample_weight: Any | None = None) -> float:
    """Return the balanced accuracy score."""
    return float(balanced_accuracy_score(y_true, y_pred, sample_weight=sample_weight))

classes staticmethod

Return the sorted set of predicted and true classes.

Source code in highfis/metrics.py
@staticmethod
def classes(
    y_true: Any,
    y_pred: Any,
    sample_weight: Any | None = None,
) -> np.ndarray:
    """Return the sorted set of predicted and true classes."""
    y_true_arr = _flatten_array(y_true)
    y_pred_arr = _flatten_array(y_pred)
    return np.unique(np.concatenate([y_true_arr, y_pred_arr]))

confusion_matrix staticmethod

Return the confusion matrix for the predictions.

Source code in highfis/metrics.py
@staticmethod
def confusion_matrix(
    y_true: Any,
    y_pred: Any,
    sample_weight: Any | None = None,
) -> np.ndarray:
    """Return the confusion matrix for the predictions."""
    y_true_arr = _flatten_array(y_true)
    y_pred_arr = _flatten_array(y_pred)
    return confusion_matrix(y_true_arr, y_pred_arr, sample_weight=sample_weight)

f1_macro staticmethod

Return macro-averaged F1 score.

Source code in highfis/metrics.py
@staticmethod
def f1_macro(y_true: Any, y_pred: Any, sample_weight: Any | None = None) -> float:
    """Return macro-averaged F1 score."""
    return float(f1_score(y_true, y_pred, average="macro", zero_division=0, sample_weight=sample_weight))

f1_micro staticmethod

Return micro-averaged F1 score.

Source code in highfis/metrics.py
@staticmethod
def f1_micro(y_true: Any, y_pred: Any, sample_weight: Any | None = None) -> float:
    """Return micro-averaged F1 score."""
    return float(f1_score(y_true, y_pred, average="micro", zero_division=0, sample_weight=sample_weight))

precision_macro staticmethod

Return macro-averaged precision.

Source code in highfis/metrics.py
@staticmethod
def precision_macro(y_true: Any, y_pred: Any, sample_weight: Any | None = None) -> float:
    """Return macro-averaged precision."""
    return float(precision_score(y_true, y_pred, average="macro", zero_division=0, sample_weight=sample_weight))

precision_micro staticmethod

Return micro-averaged precision.

Source code in highfis/metrics.py
@staticmethod
def precision_micro(y_true: Any, y_pred: Any, sample_weight: Any | None = None) -> float:
    """Return micro-averaged precision."""
    return float(precision_score(y_true, y_pred, average="micro", zero_division=0, sample_weight=sample_weight))

recall_macro staticmethod

Return macro-averaged recall.

Source code in highfis/metrics.py
@staticmethod
def recall_macro(y_true: Any, y_pred: Any, sample_weight: Any | None = None) -> float:
    """Return macro-averaged recall."""
    return float(recall_score(y_true, y_pred, average="macro", zero_division=0, sample_weight=sample_weight))

recall_micro staticmethod

Return micro-averaged recall.

Source code in highfis/metrics.py
@staticmethod
def recall_micro(y_true: Any, y_pred: Any, sample_weight: Any | None = None) -> float:
    """Return micro-averaged recall."""
    return float(recall_score(y_true, y_pred, average="micro", zero_division=0, sample_weight=sample_weight))

RegressionMetrics

Standard regression metrics.

explained_variance staticmethod

Return explained variance.

Source code in highfis/metrics.py
@staticmethod
def explained_variance(y_true: Any, y_pred: Any, sample_weight: Any | None = None) -> float:
    """Return explained variance."""
    return float(explained_variance_score(y_true, y_pred, sample_weight=sample_weight))

mae staticmethod

Return mean absolute error.

Source code in highfis/metrics.py
@staticmethod
def mae(y_true: Any, y_pred: Any, sample_weight: Any | None = None) -> float:
    """Return mean absolute error."""
    return float(mean_absolute_error(y_true, y_pred, sample_weight=sample_weight))

mape staticmethod

Return mean absolute percentage error.

Source code in highfis/metrics.py
@staticmethod
def mape(y_true: Any, y_pred: Any, sample_weight: Any | None = None) -> float:
    """Return mean absolute percentage error."""
    return float(mean_absolute_percentage_error(y_true, y_pred))

max_error staticmethod

Return maximum absolute error.

Source code in highfis/metrics.py
@staticmethod
def max_error(y_true: Any, y_pred: Any, sample_weight: Any | None = None) -> float:
    """Return maximum absolute error."""
    return float(max_error(y_true, y_pred))

mean_bias_error staticmethod

Return mean bias error (prediction minus truth).

Source code in highfis/metrics.py
@staticmethod
def mean_bias_error(y_true: Any, y_pred: Any, sample_weight: Any | None = None) -> float:
    """Return mean bias error (prediction minus truth)."""
    y_true_arr = _flatten_array(y_true)
    y_pred_arr = _flatten_array(y_pred)
    return float(np.mean(y_pred_arr - y_true_arr))

median_absolute_error staticmethod

Return median absolute error.

Source code in highfis/metrics.py
@staticmethod
def median_absolute_error(y_true: Any, y_pred: Any, sample_weight: Any | None = None) -> float:
    """Return median absolute error."""
    return float(median_absolute_error(y_true, y_pred, sample_weight=sample_weight))

mse staticmethod

Return mean squared error.

Source code in highfis/metrics.py
@staticmethod
def mse(y_true: Any, y_pred: Any, sample_weight: Any | None = None) -> float:
    """Return mean squared error."""
    return float(mean_squared_error(y_true, y_pred, sample_weight=sample_weight))

msle staticmethod

Return mean squared logarithmic error.

Source code in highfis/metrics.py
@staticmethod
def msle(y_true: Any, y_pred: Any, sample_weight: Any | None = None) -> float:
    """Return mean squared logarithmic error."""
    try:
        return float(mean_squared_log_error(y_true, y_pred))
    except ValueError:
        return float(np.nan)

pearson staticmethod

Return Pearson correlation coefficient.

Source code in highfis/metrics.py
@staticmethod
def pearson(y_true: Any, y_pred: Any, sample_weight: Any | None = None) -> float:
    """Return Pearson correlation coefficient."""
    y_true_arr = _flatten_array(y_true)
    y_pred_arr = _flatten_array(y_pred)
    if y_true_arr.size < 2 or np.std(y_true_arr) == 0 or np.std(y_pred_arr) == 0:
        return float(np.nan)
    return float(np.corrcoef(y_true_arr, y_pred_arr)[0, 1])

r2 staticmethod

Return coefficient of determination (R²).

Source code in highfis/metrics.py
@staticmethod
def r2(y_true: Any, y_pred: Any, sample_weight: Any | None = None) -> float:
    """Return coefficient of determination (R²)."""
    return float(r2_score(y_true, y_pred, sample_weight=sample_weight))

rmse staticmethod

Return root mean squared error.

Source code in highfis/metrics.py
@staticmethod
def rmse(y_true: Any, y_pred: Any, sample_weight: Any | None = None) -> float:
    """Return root mean squared error."""
    return float(np.sqrt(mean_squared_error(y_true, y_pred, sample_weight=sample_weight)))

smape staticmethod

Return symmetric mean absolute percentage error.

Source code in highfis/metrics.py
@staticmethod
def smape(y_true: Any, y_pred: Any, sample_weight: Any | None = None) -> float:
    """Return symmetric mean absolute percentage error."""
    y_true_arr = _flatten_array(y_true)
    y_pred_arr = _flatten_array(y_pred)
    numerator = np.abs(y_pred_arr - y_true_arr) * 2.0
    denominator = np.abs(y_true_arr) + np.abs(y_pred_arr)
    with np.errstate(divide="ignore", invalid="ignore"):
        ratio = np.where(denominator == 0.0, 0.0, numerator / denominator)
    return float(np.mean(ratio))

std_error staticmethod

Return the standard deviation of the errors.

Source code in highfis/metrics.py
@staticmethod
def std_error(y_true: Any, y_pred: Any, sample_weight: Any | None = None) -> float:
    """Return the standard deviation of the errors."""
    y_true_arr = _flatten_array(y_true)
    y_pred_arr = _flatten_array(y_pred)
    return float(np.std(y_pred_arr - y_true_arr))

compute_metrics

Compute a set of named evaluation metrics.

Parameters:

Name Type Description Default
task Task

"classification" or "regression".

required
y_true Any

Ground-truth labels or targets.

required
y_pred Any

Predicted labels or values.

required
sample_weight Any | None

Optional sample weights.

None
metrics list[str] | None

Optional list of metric names to compute.

None

Returns:

Type Description
dict[str, Any]

Dictionary mapping metric names to scalar float results.

Source code in highfis/metrics.py
def compute_metrics(
    task: Task,
    y_true: Any,
    y_pred: Any,
    sample_weight: Any | None = None,
    metrics: list[str] | None = None,
) -> dict[str, Any]:
    """Compute a set of named evaluation metrics.

    Args:
        task: ``"classification"`` or ``"regression"``.
        y_true: Ground-truth labels or targets.
        y_pred: Predicted labels or values.
        sample_weight: Optional sample weights.
        metrics: Optional list of metric names to compute.

    Returns:
        Dictionary mapping metric names to scalar float results.
    """
    if task == "classification":
        metric_names = (
            _validate_classification_metrics(metrics) if metrics is not None else DEFAULT_CLASSIFICATION_METRICS
        )
        y_true_arr, y_pred_arr = _ensure_classification_inputs(
            y_true,
            y_pred,
            metric_names,
        )
        results: dict[str, Any] = {}
        for metric in metric_names:
            metric_fn = getattr(ClassificationMetrics, metric)
            results[metric] = metric_fn(y_true_arr, y_pred_arr, sample_weight)
        return results

    if task == "regression":
        metric_names = _validate_regression_metrics(metrics) if metrics is not None else DEFAULT_REGRESSION_METRICS
        y_true_arr, y_pred_arr = _ensure_regression_inputs(y_true, y_pred)
        results = {
            "mse": RegressionMetrics.mse(y_true_arr, y_pred_arr, sample_weight=sample_weight),
            "mae": RegressionMetrics.mae(y_true_arr, y_pred_arr, sample_weight=sample_weight),
            "rmse": RegressionMetrics.rmse(y_true_arr, y_pred_arr, sample_weight=sample_weight),
            "median_absolute_error": RegressionMetrics.median_absolute_error(
                y_true_arr, y_pred_arr, sample_weight=sample_weight
            ),
            "mean_bias_error": RegressionMetrics.mean_bias_error(y_true_arr, y_pred_arr, sample_weight=sample_weight),
            "max_error": RegressionMetrics.max_error(y_true_arr, y_pred_arr, sample_weight=sample_weight),
            "std_error": RegressionMetrics.std_error(y_true_arr, y_pred_arr, sample_weight=sample_weight),
            "explained_variance": RegressionMetrics.explained_variance(
                y_true_arr, y_pred_arr, sample_weight=sample_weight
            ),
            "mape": RegressionMetrics.mape(y_true_arr, y_pred_arr, sample_weight=sample_weight),
            "smape": RegressionMetrics.smape(y_true_arr, y_pred_arr, sample_weight=sample_weight),
            "msle": RegressionMetrics.msle(y_true_arr, y_pred_arr, sample_weight=sample_weight),
            "pearson": RegressionMetrics.pearson(y_true_arr, y_pred_arr, sample_weight=sample_weight),
            "r2": RegressionMetrics.r2(y_true_arr, y_pred_arr, sample_weight=sample_weight),
        }
        return {key: results[key] for key in metric_names}

    raise ValueError("task must be 'classification' or 'regression'")