All problems
HardDebuggingv2026-06-30

Debugging · Hard

Confusion Matrix by Group

Debug deterministic grouped TN, FP, FN, and TP counts.

Task

Implement confusion_matrix_by_group(labels: np.ndarray, scores: np.ndarray, groups: np.ndarray, group_order: np.ndarray, threshold: float) -> np.ndarray. Debug the grouped binary confusion-matrix implementation so it returns exact counts for every requested group in the specified order.

Requirements

  • labels, scores, and groups are aligned one-dimensional arrays of equal length. labels contains only 0 and 1, and scores contains finite numeric values.
  • group_order is a one-dimensional array of distinct group identifiers. It defines both the groups to report and their exact row order; do not sort or infer this order from groups.
  • Predict the positive class when score >= threshold and the negative class when score < threshold. A score exactly equal to threshold is positive.
  • For each requested group, count only examples whose group identifier equals that row's identifier. Examples in groups absent from group_order are ignored.
  • Return one row per requested group with columns in the exact order [TN, FP, FN, TP].
  • A requested group with no examples contributes [0, 0, 0, 0]. If the sample arrays are empty, still return one zero row per requested group.
  • Return a two-dimensional integer array of shape (len(group_order), 4), including shapes (1, 4) for one group and (0, 4) for no requested groups.
  • Do not mutate any input.

Example

confusion_matrix_by_group(
    np.array([0, 1, 1, 0]),
    np.array([0.1, 0.9, 0.3, 0.8]),
    np.array([4, 4, 4, 4]),
    np.array([4]),
    0.5,
)
# array([[1, 1, 1, 1]])
solution.pySign in to save
Public tests run locally in your browser.
Run your code to see public test results.