# helia_edge.metrics.metric_utils

## Metrics Utils API

This module provides utility functions to compute metrics for classification tasks.

**Functions**

| Name | Description |
| --- | --- |
| `compute_metrics` | Compute set of metrics for y_true and y_pred |
| `confusion_matrix` | Compute confusion matrix using keras w/ addition to normalize |

## helia_edge.metrics.metric_utils.logger

`attribute` · `python`

```python
logger = logging.getLogger(__name__)
```

Source: `helia_edge/metrics/metric_utils.py:18`

## helia_edge.metrics.metric_utils.compute_metrics

`function` · `python`

```python
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.

Example:

```python
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)
```

**Parameters**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| metrics | list[keras.Metric] | Required | List of metrics |
| y_true | keras.KerasTensor | Required | True labels |
| y_pred | keras.KerasTensor | Required | Predicted labels |

**Returns**

| Name | Type | Description |
| --- | --- | --- |
| dict | dict[str, float] | Dictionary of metric names and values |

Source: `helia_edge/metrics/metric_utils.py:21`

## helia_edge.metrics.metric_utils.confusion_matrix

`function` · `python`

```python
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.

:::note[Normalization modes]
- "true": Normalize by true labels
- "pred": Normalize by predicted labels
- "all": Normalize by all labels

:::

Example:

```python
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)
```

**Parameters**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| labels | keras.KerasTensor | Required | True labels |
| predictions | keras.KerasTensor | Required | Predicted labels |
| num_classes | int | Required | Number of classes |
| weights | keras.KerasTensor | None | Weights. Defaults to None. |
| dtype | str | 'int32' | Data type. Defaults to "int32". |
| normalize | Literal['true', 'pred', 'all'] | None | Normalization mode. Defaults to None. |

**Returns**

| Name | Type | Description |
| --- | --- | --- |
|  | keras.KerasTensor | keras.KerasTensor: Confusion matrix |

Source: `helia_edge/metrics/metric_utils.py:55`
