Confusion matrixEasy

Confusion matrix

Background

The confusion matrix is the foundation of classification evaluation: entry (i,j)(i, j) counts how many examples of true class ii were predicted as class jj. The diagonal holds the correct predictions; everything off-diagonal is a specific kind of mistake. Accuracy, precision, recall, and F1 are all read directly off it.

Problem statement

Implement confusion_matrix(y_true, y_pred, num_classes=None) returning the C×CC \times C matrix M where M[i, j] is the number of samples with true label ii and predicted label jj. If num_classes is None, infer it as one more than the largest label observed.

Input

  • y_true — array-like of integer labels (ground truth).
  • y_pred — array-like of integer labels (predictions), same length.
  • num_classes — optional int; if omitted, inferred from the data.

Output

Returns an np.ndarray of shape (C, C) of integer counts — rows index the true class, columns the predicted class.

Examples

Example 1

Input:  y_true = [1, 0, 1, 1, 0], y_pred = [1, 0, 0, 1, 1]
Output: [[1, 1], [1, 2]]

Explanation: of the true-0 samples, 1 was predicted 0 and 1 predicted 1 (top row); of the true-1 samples, 1 was predicted 0 and 2 predicted 1 (bottom row).

Constraints

  • Rows index the true class, columns the predicted class (the sklearn convention).
  • Entries are integer counts; the matrix is (C, C) with C = num_classes.
  • Works for any number of classes, not only binary.

Notes

  • Precision of class jj is Mjj/iMijM_{jj} / \sum_i M_{ij} (down a column); recall of class ii is Mii/jMijM_{ii} / \sum_j M_{ij} (across a row).
  • The grand total equals the number of samples, and the trace equals the number of correct predictions.
Python
Loading...

This problem ships 4 hidden tests. They run in your browser via Pyodide — no backend, no submission queue. Press ▶ Run tests to execute.

  • Binary example
  • Perfect predictions give a diagonal matrix
  • Row sums equal the true-class counts
  • num_classes pads the matrix to the requested size