# Binaryconfusionmatrix

> Compute the BinaryConfusionMatrix metric — provided by torchmetrics. Use when the user has predictions and ground-truth and needs to compute BinaryConfusionMatrix, or asks how to score with BinaryConfusionMatrix.

- Skill: `qhjqhj00/binaryconfusionmatrix` (Agent Skill)
- Install (CLI): `npx skillmds add qhjqhj00/binaryconfusionmatrix`
- Raw SKILL.md: https://api.skillmd.com/api/skills/qhjqhj00/binaryconfusionmatrix/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: qhjqhj00 (https://skillmd.com/u/qhjqhj00)
- Updated: 2026-09-08
- Page: https://skillmd.com/skills/qhjqhj00/binaryconfusionmatrix

---


# binaryconfusionmatrix

> Metric `BinaryConfusionMatrix` from `torchmetrics` (torchmetrics.classification.BinaryConfusionMatrix)

## When to invoke this skill

The user has predictions + ground truth and asks to evaluate with BinaryConfusionMatrix, or
mentions `torchmetrics.classification.BinaryConfusionMatrix` directly, or wants the standard torchmetrics implementation.

## Reference signature

```python
from torchmetrics.classification import BinaryConfusionMatrix

# BinaryConfusionMatrix(threshold: float = 0.5, ignore_index: Optional[int] = None, normalize: Optional[Literal['true', 'pred', 'all', 'none']] = None, validate_args: bool = True, **kwargs: Any) -> None
```

## Library docstring

```
Compute the `confusion matrix`_ for binary tasks.

The confusion matrix :math:`C` is constructed such that :math:`C_{i, j}` is equal to the number of observations
known to be in class :math:`i` but predicted to be in class :math:`j`. Thus row indices of the confusion matrix
correspond to the true class labels and column indices correspond to the predicted class labels.

For binary tasks, the confusion matrix is a 2x2 matrix with the following structure:

- :math:`C_{0, 0}`: True negatives
- :math:`C_{0, 1}`: False positives
- :math:`C_{1, 0}`: False negatives
- :math:`C_{1, 1}`: True positives

As input to ``forward`` and ``update`` the metric accepts the following input:

- ``preds`` (:class:`~torch.Tensor`): An int or float tensor of shape ``(N, ...)``. If preds is a floating point
  tensor with values outside [0,1] range we consider the input to be logits and will auto apply sigmoid per
  element. Additionally, we convert to int tensor with thresholding using the value in ``threshold``.
- ``target`` (:class:`~torch.Tensor`): An int tensor of shape ``(N, ...)``.

As output to ``forward`` and ``compute`` the metric returns the following output:

- ``confusion_matrix`` (:class:`~torch.Tensor`): A tensor containing a ``(2, 2)`` matrix

Additional dimension ``...`` will be flattened into the batch dimension.

Args:
    threshold: Threshold for transforming probability to binary (0,1) predictions
    ignore_index:
        Specifies a target value that is ignored and does not contribute to the metric calculation
    normalize: Normalization mode for confusion matrix. Choose from:

        - ``None`` or ``'none'``: no normalization (default)
        - ``'true'``: normalization over the targets (most commonly used)
        - ``'pred'``: normalization over the predictions
        - ``'all'``: normalization over the whole matrix
    validate_args: bool indicating if input arguments and tensors should be validated for correctness.
        Set to ``False`` for faster computations.
    kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.

Example (preds is int tensor):
    >>> from torchmetrics.classification import BinaryConfusionMatrix
    >>> ta
```

## Quick recipe

```python
import torchmetrics.classification as _m
score = _m.BinaryConfusionMatrix(y_true, y_pred)
```

## Don'ts

- Don't reimplement when the library version handles edge cases (NaN, ties, empty inputs) better than a hand-rolled formula.
- Always check the library version's argument order — sklearn is `(y_true, y_pred)` while torchmetrics is `(preds, target)`.

