learn deep. ship safe.Practice testsFollow ByteMunSubscribe

mediumMetrics

Multiclass Confusion Matrix

Build a confusion matrix for any number of classes from true and predicted labels, the table every classification metric is derived from.

JavaScript or Python

A confusion matrix counts, for every pair of classes, how many examples of actual class i were predicted as class j. Given y_true, y_pred and the list of class labels (which fixes the row and column order), return the matrix as a list of rows: row i is the actual class labels[i] and column j is the predicted class labels[j].

Labels are strings. Every value in y_true and y_pred appears in labels. A class that never occurs still gets its row and column of zeros.

Examples

Example 1

Input: y_true = ["cat", "dog", "cat"], y_pred = ["cat", "cat", "dog"], labels = ["cat", "dog"]
Output: [[1, 1], [1, 0]]
Why: One cat right, one cat called dog, one dog called cat

Example 2

Input: y_true = ["a", "b"], y_pred = ["a", "b"], labels = ["a", "b", "c"]
Output: [[1, 0, 0], [0, 1, 0], [0, 0, 0]]
Why: Class c never appears but keeps its row and column

Constraints

  • 1 <= labels.length <= 100
  • 0 <= y_true.length == y_pred.length <= 10^5

Hints

Hint 1

Map each label to its position first, so lookups are O(1).

Hint 2

Start from a matrix of zeros and add one per (actual, predicted) pair.

Solution

Show the solution (try it yourself first)

Map each label to its index, create a k by k matrix of zeros, then for every pair increment matrix[actual][predicted]. The diagonal holds correct predictions; everything off the diagonal is a specific kind of mistake.

def confusion_matrix(y_true, y_pred, labels):
    index = {label: i for i, label in enumerate(labels)}
    matrix = [[0] * len(labels) for _ in labels]
    for t, p in zip(y_true, y_pred):
        matrix[index[t]][index[p]] += 1
    return matrix

Complexity

Time O(n + k²) for n examples and k classes, space O(k²). From this table you get per-class precision (diagonal over column sum) and recall (diagonal over row sum), and macro or weighted averages for multiclass reporting. Interviewers often ask which off-diagonal cell matters most for a given business problem, so be ready to read the matrix, not only build it.

Found a mistake in this problem, its tests or the solution?

Written by Published Tests checked by Deepak
Case 1Input: ["cat","dog","cat"], ["cat","cat","dog"], ["cat","dog"]Expected: [[1,1],[1,0]]
Case 2Input: ["a","b"], ["a","b"], ["a","b","c"]Expected: [[1,0,0],[0,1,0],[0,0,0]]

Run checks these 2 samples. Submit checks all 5 tests, including hidden ones.

  1. easy
  2. Maximum Subarray SumArrays · Dynamic programming
    medium
  3. Longest Substring Without RepeatsStrings · Sliding window · Hash map
    medium
  4. Group AnagramsStrings · Hash map · Sorting
    medium