roboflow/supervision · error · ValueError

Confusion matrix must have shape (..., 3), got {confusion_ma

Error message

Confusion matrix must have shape (..., 3), got {confusion_matrix.shape}

What it means

Error "Confusion matrix must have shape (..., 3), got {confusion_matrix.shape}" thrown in roboflow/supervision.

Source

Thrown at src/supervision/metrics/f1_score.py:499

        result_confusion_matrix: npt.NDArray[np.float64] = confusion_matrix
        return result_confusion_matrix

    @staticmethod
    def _compute_f1(
        confusion_matrix: npt.NDArray[np.float64],
    ) -> npt.NDArray[np.float64]:
        """
        Broadcastable function, computing the F1 score from the confusion matrix.

        Args:
            confusion_matrix: shape (N, ..., 3), where the last dimension
                contains the true positives, false positives, and false negatives.

        Returns:
            shape (N, ...), containing the F1 score for each element.
        """
        if not confusion_matrix.shape[-1] == 3:
            raise ValueError(
                f"Confusion matrix must have shape (..., 3), got "
                f"{confusion_matrix.shape}"
            )
        true_positives = confusion_matrix[..., 0]
        false_positives = confusion_matrix[..., 1]
        false_negatives = confusion_matrix[..., 2]

        # Alternate formula, avoids multiple zero division checks
        denominator = 2 * true_positives + false_positives + false_negatives
        f1_score = np.divide(
            2 * true_positives,
            denominator,
            out=np.zeros_like(denominator, dtype=np.float64),
            where=denominator != 0,
        )

        result_f1_score: npt.NDArray[np.float64] = f1_score
        return result_f1_score

View on GitHub (pinned to 7f254d9784)

Solutions

  1. Provide a confusion matrix whose last dimension is 3.
  2. Use the metric's own computation methods to build the matrix instead of constructing it manually.

When it happens

Trigger: Thrown at src/supervision/metrics/f1_score.py:499 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of roboflow/supervision@7f254d9784 (2026-08-15). Data as JSON: /api/errors/85e8c363993ecd8d. Report an issue: GitHub.