Confusion matrix

Write codeMedium20 minMeta style

Your task

Build a confusion matrix for a multiclass problem. Given equal-length lists of predicted and true integer labels and an integer num_classes (labels range over 0..num_classes-1), return a num_classes x num_classes matrix M where M[t][p] is the number of samples whose true label is t and predicted label is p. Rows are indexed by true label, columns by predicted label. Inputs may be empty (return an all-zero matrix of the right shape).

Examples

confusion_matrix(preds: list[int], labels: list[int], num_classes: int) -> list[list[int]]
Example 1
Inputconfusion_matrix([0, 1, 1, 0], [0, 1, 0, 0], 2)
Output[[2, 1], [0, 1]]