Confusion matrix
Background
The confusion matrix is the foundation of classification evaluation: entry counts how many examples of true class were predicted as class . 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 matrix M where M[i, j] is the number of samples with true label and predicted label . 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— optionalint; 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)withC = num_classes. - Works for any number of classes, not only binary.
Notes
- Precision of class is (down a column); recall of class is (across a row).
- The grand total equals the number of samples, and the trace equals the number of correct predictions.
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