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
¶
Compute structural statistics for an ANFIS model instance.
regression_metrics
staticmethod
¶
Return a suite of regression metrics for predictions vs. targets.
MetricReport
dataclass
¶
accuracy ¶
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 ¶
Return the macro-average recall, balancing performance across classes.
classification_entropy ¶
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 ¶
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 ¶
Compute the explained variance score for regression predictions.
log_loss ¶
Compute mean log loss from integer/one-hot labels and probabilities.
mean_absolute_error ¶
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 |
False
|
Returns:
| Type | Description |
|---|---|
float
|
MAPE value as a percentage (float). |
mean_bias_error ¶
Compute the mean signed error, positive when predictions overshoot.
mean_squared_error ¶
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 ¶
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 ¶
Return the median absolute deviation between predictions and targets.
partition_coefficient ¶
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 ¶
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 ¶
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 ¶
Compute the root mean squared error (RMSE).
This is simply the square root of mean_squared_error.
softmax ¶
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:
mean_squared_error()- Mean squared errormean_absolute_error()- Mean absolute errorroot_mean_squared_error()- Root mean squared errormean_absolute_percentage_error()- Mean absolute percentage errorsymmetric_mean_absolute_percentage_error()- Symmetric MAPEr2_score()- Coefficient of determinationexplained_variance_score()- Explained variance of predictionsmedian_absolute_error()- Median absolute error (robust to outliers)mean_bias_error()- Mean prediction biaspearson_correlation()- Pearson correlation coefficientmean_squared_logarithmic_error()- Mean squared logarithmic error
Classification Metrics¶
Functions for evaluating classification model performance:
softmax()- Numerically stable softmaxcross_entropy()- Cross-entropy losslog_loss()- Log lossaccuracy()- Classification accuracybalanced_accuracy_score()- Macro average of per-class recallprecision_recall_f1()- Precision, recall, and F1 with macro/micro/binary averaging
Clustering Validation¶
Functions for evaluating fuzzy clustering quality:
partition_coefficient()- Bezdek's partition coefficientclassification_entropy()- Classification entropyxie_beni_index()- Xie-Beni validity index
Metric Reports & Automation¶
compute_metrics()- One-stop helper that infers the task (regression vs. classification) and returns aMetricReportMetricReport- Read-only container with attribute/dict-style access and a.to_dict()export