Skip to content

Metrics API

anfis_toolbox.metrics

Common metrics utilities for ANFIS Toolbox.

This module provides lightweight, dependency-free metrics that are useful for training and evaluating ANFIS models.

ANFISMetrics

Metrics calculator utilities for ANFIS models.

classification_metrics staticmethod

classification_metrics(
    y_true: ArrayLike,
    y_pred: ArrayLike | None = None,
    *,
    y_proba: ArrayLike | None = None,
    logits: ArrayLike | None = None,
) -> dict[str, MetricValue]

Return common classification metrics for encoded targets and predictions.

model_complexity_metrics staticmethod

model_complexity_metrics(model: TSKANFIS) -> dict[str, int]

Compute structural statistics for an ANFIS model instance.

regression_metrics staticmethod

regression_metrics(
    y_true: ArrayLike, y_pred: ArrayLike
) -> dict[str, MetricValue]

Return a suite of regression metrics for predictions vs. targets.

MetricReport dataclass

MetricReport(
    task: Literal["regression", "classification"],
    _values: Mapping[str, MetricValue],
)

Immutable container exposing computed metrics by key or attribute.

__getattr__

__getattr__(item: str) -> MetricValue

Allow attribute-style access to stored metrics.

__getitem__

__getitem__(key: str) -> MetricValue

Provide dictionary-style access to metric values.

__post_init__

__post_init__() -> None

Sanitize stored NumPy scalars/arrays to prevent accidental mutation.

keys

keys() -> Iterable[str]

Expose the metric key iterator from the backing mapping.

to_dict

to_dict() -> dict[str, MetricValue]

Return a shallow copy of the underlying metric mapping.

accuracy

accuracy(y_true: ndarray, y_pred: ndarray) -> float

Compute accuracy from integer/one-hot labels and logits/probabilities.

y_pred can be class indices (n,), logits (n,k), or probabilities (n,k). y_true can be class indices (n,) or one-hot (n,k).

balanced_accuracy_score

balanced_accuracy_score(
    y_true: ndarray, y_pred: ndarray
) -> float

Return the macro-average recall, balancing performance across classes.

classification_entropy

classification_entropy(
    U: ndarray, epsilon: float = 1e-12
) -> float

Classification Entropy (CE). Lower is better (crisper).

Parameters:

Name Type Description Default
U ndarray

Membership matrix of shape (n_samples, n_clusters).

required
epsilon float

Small constant to avoid log(0).

1e-12

Returns:

Type Description
float

CE value as float.

compute_metrics

compute_metrics(
    y_true: ArrayLike,
    *,
    y_pred: ArrayLike | None = None,
    y_proba: ArrayLike | None = None,
    logits: ArrayLike | None = None,
    task: Literal[
        "auto", "regression", "classification"
    ] = "auto",
    metrics: Sequence[str] | None = None,
    custom_metrics: Mapping[str, MetricFn] | None = None,
) -> MetricReport

Compute regression or classification metrics and return a report.

cross_entropy

cross_entropy(
    y_true: ndarray,
    logits: ndarray,
    epsilon: float = _EPSILON,
) -> float

Compute mean cross-entropy from integer labels or one-hot vs logits.

Parameters:

Name Type Description Default
y_true ndarray

Array-like of shape (n_samples,) of integer class labels, or one-hot array of shape (n_samples, n_classes).

required
logits ndarray

Array-like raw scores, shape (n_samples, n_classes).

required
epsilon float

Small constant for numerical stability.

_EPSILON

Returns:

Type Description
float

Mean cross-entropy (float).

explained_variance_score

explained_variance_score(
    y_true: ndarray,
    y_pred: ndarray,
    epsilon: float = _EPSILON,
) -> float

Compute the explained variance score for regression predictions.

log_loss

log_loss(
    y_true: ndarray,
    y_prob: ndarray,
    epsilon: float = _EPSILON,
) -> float

Compute mean log loss from integer/one-hot labels and probabilities.

mean_absolute_error

mean_absolute_error(
    y_true: ndarray, y_pred: ndarray
) -> float

Compute the mean absolute error (MAE).

Parameters:

Name Type Description Default
y_true ndarray

Array-like of true target values, shape (...,)

required
y_pred ndarray

Array-like of predicted values, same shape as y_true

required

Returns:

Type Description
float

The mean of absolute differences over all elements as a float.

Notes
  • Inputs are coerced to NumPy arrays with dtype=float.
  • Broadcasting follows NumPy semantics. If shapes are not compatible for element-wise subtraction, a ValueError will be raised by NumPy.

mean_absolute_percentage_error

mean_absolute_percentage_error(
    y_true: ndarray,
    y_pred: ndarray,
    epsilon: float = 1e-12,
    *,
    ignore_zero_targets: bool = False,
) -> float

Compute the mean absolute percentage error (MAPE) in percent.

MAPE = mean( abs((y_true - y_pred) / max(abs(y_true), epsilon)) ) * 100

Parameters:

Name Type Description Default
y_true ndarray

Array-like of true target values.

required
y_pred ndarray

Array-like of predicted values, broadcastable to y_true.

required
epsilon float

Small constant to avoid division by zero when y_true == 0.

1e-12
ignore_zero_targets bool

When True, drop samples where |y_true| <= epsilon; if all targets are (near) zero, returns np.inf to signal undefined percentage.

False

Returns:

Type Description
float

MAPE value as a percentage (float).

mean_bias_error

mean_bias_error(y_true: ndarray, y_pred: ndarray) -> float

Compute the mean signed error, positive when predictions overshoot.

mean_squared_error

mean_squared_error(
    y_true: ndarray, y_pred: ndarray
) -> float

Compute the mean squared error (MSE).

Parameters:

Name Type Description Default
y_true ndarray

Array-like of true target values, shape (...,)

required
y_pred ndarray

Array-like of predicted values, same shape as y_true

required

Returns:

Type Description
float

The mean of squared differences over all elements as a float.

Notes
  • Inputs are coerced to NumPy arrays with dtype=float.
  • Broadcasting follows NumPy semantics. If shapes are not compatible for element-wise subtraction, a ValueError will be raised by NumPy.

mean_squared_logarithmic_error

mean_squared_logarithmic_error(
    y_true: ndarray, y_pred: ndarray
) -> float

Compute the mean squared logarithmic error (MSLE).

Requires non-negative inputs. Uses log1p for numerical stability: MSLE = mean( (log1p(y_true) - log1p(y_pred))^2 ).

median_absolute_error

median_absolute_error(
    y_true: ndarray, y_pred: ndarray
) -> float

Return the median absolute deviation between predictions and targets.

partition_coefficient

partition_coefficient(U: ndarray) -> float

Bezdek's Partition Coefficient (PC) in [1/k, 1]. Higher is crisper.

Parameters:

Name Type Description Default
U ndarray

Membership matrix of shape (n_samples, n_clusters).

required

Returns:

Type Description
float

PC value as float.

pearson_correlation

pearson_correlation(
    y_true: ndarray,
    y_pred: ndarray,
    epsilon: float = _EPSILON,
) -> float

Compute the Pearson correlation coefficient r.

Returns 0.0 when the standard deviation of either input is ~0 (undefined r).

precision_recall_f1

precision_recall_f1(
    y_true: ArrayLike,
    y_pred: ArrayLike,
    average: Literal["macro", "micro", "binary"] = "macro",
) -> tuple[float, float, float]

Compute precision, recall, and F1 score with the requested averaging.

quick_evaluate

quick_evaluate(
    model: object,
    X_test: ndarray,
    y_test: ndarray,
    print_results: bool = True,
    task: Literal[
        "auto", "regression", "classification"
    ] = "auto",
) -> dict[str, float]

Evaluate a trained ANFIS model or estimator on test data.

r2_score

r2_score(
    y_true: ndarray,
    y_pred: ndarray,
    epsilon: float = _EPSILON,
) -> float

Compute the coefficient of determination R^2.

R^2 = 1 - SS_res / SS_tot, where SS_res = sum((y - y_hat)^2) and SS_tot = sum((y - mean(y))^2). If SS_tot is ~0 (constant target), returns 1.0 when predictions match the constant target (SS_res ~0), otherwise 0.0.

root_mean_squared_error

root_mean_squared_error(
    y_true: ndarray, y_pred: ndarray
) -> float

Compute the root mean squared error (RMSE).

This is simply the square root of mean_squared_error.

softmax

softmax(logits: ndarray, axis: int = -1) -> np.ndarray

Compute a numerically stable softmax along a given axis.

symmetric_mean_absolute_percentage_error

symmetric_mean_absolute_percentage_error(
    y_true: ndarray,
    y_pred: ndarray,
    epsilon: float = _EPSILON,
) -> float

Compute the symmetric mean absolute percentage error (SMAPE) in percent.

SMAPE = mean( 200 * |y_true - y_pred| / (|y_true| + |y_pred|) ) with an epsilon added to denominator to avoid division by zero.

Parameters:

Name Type Description Default
y_true ndarray

Array-like of true target values.

required
y_pred ndarray

Array-like of predicted values, broadcastable to y_true.

required
epsilon float

Small constant added to denominator to avoid division by zero.

_EPSILON

Returns:

Type Description
float

SMAPE value as a percentage (float).

xie_beni_index

xie_beni_index(
    X: ndarray,
    U: ndarray,
    C: ndarray,
    m: float = 2.0,
    epsilon: float = 1e-12,
) -> float

Xie-Beni index (XB). Lower is better.

XB = sum_i sum_k u_ik^m ||x_i - v_k||^2 / (n * min_{p!=q} ||v_p - v_q||^2)

Parameters:

Name Type Description Default
X ndarray

Data array, shape (n_samples, n_features) or (n_samples,).

required
U ndarray

Membership matrix, shape (n_samples, n_clusters).

required
C ndarray

Cluster centers, shape (n_clusters, n_features).

required
m float

Fuzzifier (>1).

2.0
epsilon float

Small constant to avoid division by zero.

1e-12

Returns:

Type Description
float

XB value as float (np.inf when centers < 2).

This module provides comprehensive metrics for evaluating ANFIS models across regression, classification, and clustering tasks.

Regression Metrics

Functions for evaluating regression model performance:

Classification Metrics

Functions for evaluating classification model performance:

Clustering Validation

Functions for evaluating fuzzy clustering quality:

Metric Reports & Automation

  • compute_metrics() - One-stop helper that infers the task (regression vs. classification) and returns a MetricReport
  • MetricReport - Read-only container with attribute/dict-style access and a .to_dict() export