metric_utils
Metrics Utils API
This module provides utility functions to compute metrics for classification tasks.
Functions:
-
compute_metrics–Compute set of metrics for y_true and y_pred
-
confusion_matrix–Compute confusion matrix using keras w/ addition to normalize
Functions
compute_metrics
compute_metrics(metrics: list[keras.Metric], y_true: keras.KerasTensor, y_pred: keras.KerasTensor) -> dict[str, float]
Compute set of metrics for y_true and y_pred.
Parameters:
-
(metricslist[Metric]) –List of metrics
-
(y_trueKerasTensor) –True labels
-
(y_predKerasTensor) –Predicted labels
Returns:
Example:
import helia_edge as helia
metrics = [keras.metrics.Accuracy('acc'), keras.metrics.Precision(0.5, name='precision')]
y_true = np.array([0, 1, 1, 0])
y_pred = np.array([0, 1, 0, 1])
results = helia.metrics.compute_metrics(metrics, y_true, y_pred)
print(results)
Source code in helia_edge/metrics/metric_utils.py
confusion_matrix
confusion_matrix(labels: keras.KerasTensor, predictions: keras.KerasTensor, num_classes: int, weights: keras.KerasTensor | None = None, dtype: str = 'int32', normalize: Literal['true', 'pred', 'all'] | None = None) -> keras.KerasTensor
Compute confusion matrix using keras w/ addition to normalize.
Normalization modes
- "true": Normalize by true labels
- "pred": Normalize by predicted labels
- "all": Normalize by all labels
Parameters:
-
(labelsKerasTensor) –True labels
-
(predictionsKerasTensor) –Predicted labels
-
(num_classesint) –Number of classes
-
(weightsKerasTensor, default:None) –Weights. Defaults to None.
-
(dtypestr, default:'int32') –Data type. Defaults to "int32".
-
(normalizeLiteral['true', 'pred', 'all'], default:None) –Normalization mode. Defaults to None.
Returns:
-
KerasTensor–keras.KerasTensor: Confusion matrix
Example:
import helia_edge as helia
labels = np.array([0, 1, 1, 0])
predictions = np.array([0, 1, 0, 1])
num_classes = 2
cm = helia.metrics.confusion_matrix(labels, predictions, num_classes, normalize='true')
print(cm)