Problems
Loading...
1 / 1

Compute Confusion Matrix with Normalization

Metrics & Evaluation
Hard

Compute a K×K confusion matrix where rows are true labels and columns are predictions. Support multiple normalization modes for different analysis perspectives.

A confusion matrix is a fundamental tool for evaluating classification performance. It provides detailed insights into which classes are being confused with each other, enabling targeted model improvements.

Confusion Matrix Definition:

Cij=n=1N1 ⁣[ytrue(n)=i   and   ypred(n)=j]C_{ij} = \sum_{n=1}^{N} \mathbf{1}\!\left[\, y_{\text{true}}^{(n)} = i \;\text{ and }\; y_{\text{pred}}^{(n)} = j \,\right]

Normalization Modes:

'none': Raw counts

'true': Row-wise → jCij=1∑_{j}C_{ij}=1

'pred': Column-wise → iCij=1∑_{i}C_{ij}=1

'all': Total → ijCij=1∑_{ij}C_{ij}=1

Function Arguments

  • y_true: array-like, shape (N,) - True class labels, integers in [0, K-1]
  • y_pred: array-like, shape (N,) - Predicted class labels, integers in [0, K-1]
  • num_classes: optional int - Number of classes K; if None, infer from max(labels)+1
  • normalize: str - Normalization mode: 'none', 'true', 'pred', or 'all'
Loading visualization...

Examples

Input: y_true=[0,1,1], y_pred=[0,1,0], normalize='none'

Output: [[1,0],[1,1]]

Input: y_true=[0,1,1], y_pred=[0,1,0], normalize='true'

Output: [[1.0,0.0],[0.5,0.5]]

Hint 1

Use np.bincount() with indices computed as y_true * K + y_pred.

Hint 2

For normalization, use keepdims=True in np.sum() to maintain dimensions.

Hint 3

Handle division by zero by setting zero denominators to 1 before division.

Requirements

  • Return K×K numpy array (int for 'none', float for normalized)
  • Use vectorized bincount over (true*K + pred) indices
  • Handle edge cases: empty arrays, single class, extra classes
  • Add small epsilon to avoid division by zero in normalization
  • Validate that all labels are in valid range [0, K-1]

Constraints

  • N ≤ 1e7, K ≤ 1000; NumPy only
  • Time limit: 300ms; Memory: 128MB
Try Similar Problems