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:
-
metrics
(list[Metric]
) –List of metrics
-
y_true
(KerasTensor
) –True labels
-
y_pred
(KerasTensor
) –Predicted labels
Returns:
Example:
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 = nse.metrics.compute_metrics(metrics, y_true, y_pred)
print(results)
Source code in neuralspot_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:
-
labels
(KerasTensor
) –True labels
-
predictions
(KerasTensor
) –Predicted labels
-
num_classes
(int
) –Number of classes
-
weights
(KerasTensor
, default:None
) –Weights. Defaults to None.
-
dtype
(str
, default:'int32'
) –Data type. Defaults to "int32".
-
normalize
(Literal['true', 'pred', 'all']
, default:None
) –Normalization mode. Defaults to None.
Returns:
-
KerasTensor
–keras.KerasTensor: Confusion matrix
Example:
labels = np.array([0, 1, 1, 0])
predictions = np.array([0, 1, 0, 1])
num_classes = 2
cm = nse.metrics.confusion_matrix(labels, predictions, num_classes, normalize='true')
print(cm)