Start learning free

LESSON 81 OF 165 · MACHINE LEARNING: ML INTERVIEW QUESTIONS

Build a Confusion Matrix in Python

Practice

An interviewer asks: "Build a confusion matrix from scratch." The idea in one sentence: a confusion matrix is a k by k table of counts where row r, column c counts how often the real class r was predicted as c.

Three numpy tools do it. np.zeros((k, k), dtype=int) gives back k rows of k zeros, kept as whole numbers. matrix[r, c] reads or changes the cell at row r, column c, so matrix[r, c] += 1 adds one to it. np.add.at(a, (rows, cols), 1) adds 1 at every (row, column) pair at once and counts a pair again each time it repeats; it changes a in place and gives back nothing.

grid = np.zeros((2, 3), dtype=int)
grid[1, 2] += 1
np.add.at(grid, ([0, 0], [1, 1]), 1)
print(grid)
# [[0 2 0]
#  [0 0 1]]

The starting code has 10 real classes in y_true and the model's 10 guesses in y_pred, for 3 classes. You build the table twice: once with a loop, once with np.add.at. The script must print:

With a loop:
[[3 0 0]
 [0 2 1]
 [1 1 2]]
With np.add.at:
[[3 0 0]
 [0 2 1]
 [1 1 2]]
Plain matrix[y_true, y_pred] += 1 adds each pair only once:
[[1 0 0]
 [0 1 1]
 [1 1 1]]
No class 2 in the labels, k = 3: [[0, 1, 0], [0, 1, 0], [0, 0, 0]]
Same as scikit-learn's confusion_matrix on 200 random labels: True True

The lines after your two tables come from the check. The plain matrix[y_true, y_pred] += 1 counts a repeated pair only once, which is why the fast version needs np.add.at.

Your task: fill in confusion with a zero matrix and a loop that adds 1 at row y_true[i], column y_pred[i]. Then fill in confusion_fast with a zero matrix and one call to np.add.at.


Assignment
Follow the Coding Tutorial to build the confusion matrix with a loop, then without one.


Hint
Rows are the real classes and columns the predicted ones, so the real class always comes first in the brackets. np.add.at works in place: call it on its own line and don't store what it gives back.


STEP 1 OF 5
    Ctrl + Enter to check

    Mlq Confusion Matrix in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free